Powered by Blogger.

Java Programming | Declaring Classes, Attributes, Constructors, and Methods

Declaring Classes, Attributes, Constructors, and Methods:

A class is a software blueprint. A class defines the set of data elements (called attributes), as well as the set of behaviors or functions (called methods). Together, attributes and methods are called members of a class. In Java technology, class name usually starts with a capital letter, attribute and method name usually starts with a small letter. The access modifier for the classes in the Java technology can be public or default. The Java technology class, attribute, constructor, and method declaration.

A constructor is a set of instructions designed to initialize an instance or object. The name of the constructor must always be the same as the class name. Constructor do not return any value. Valid modifiers for constructors are public, protected, and private.

The Default Constructor: Every Java class has at least one constructor. If you do not write a constructor in a class then Java technology provides a default constructor for you. This constructor takes no arguments and has an empty body.

Example: 

 public class MyDate{
 private int day;
 private int month;
 private int year;
 public MyDate(){
 day=1;
 month=6;
 year=1979;
 }
 public void showDate(){
 System.out.println(”Day:”+day);
 System.out.println(”Month:”+month);
 System.out.println(”Year:”+year);
 }
 public static void main(String[ ]args){
 MyDate date = new MyDate();
 date.showDate();
 }
 }

0 comments:

Post a Comment