Constructor methods initialize new objects when they are created. Unlike regular methods, constructor methods cannot be called directly. They are called automatically when a new object is created. When an object is created in Java using the keyword new the following things happen:
• Memory is allocated for the new object.
• Instance variables of the object are initialized, either to their initial values or to default values (0 for numbers, null for objects, false for boolean, '\0' for characters.)
• A constructor method is invoked. (A class may have one or more constructor methods with different arguments lists. Based on the number of parameters and their data types, the corresponding constructor will be invoked.)
If a class does not have any defined special constructor methods, the instance variables of the new object should be set or other methods that are needed by the object to initialize itself should be called.
By defining constructor methods in classes, the initial values of instance variables can be set. Java also facilitates overloading of constructors. Constructor overloading helps to create an object that has specific properties defined on the basis of the arguments given in the new expression.
Constructors look a lot like regular methods, with two basic differences:
• Constructors always have the same name as the class.
• Constructors do not have a return type .
Program shows a simple class by the name Identity. The constructor method for Identity takes two arguments: a string object representing its name and an integer that stands for its age.
Identity (String n, int a)
name = n;
age = a;
System.out.print("Name is" + name);
System.out.printin(".Age is" + age);
public static void main (String args)
I = new Identity("Shyam", 30);
System.out.println(" -------- ") ;
I = new Identity("Johnny", 3);
I.print Identity ();
System.out.printIn(" -------- ") ;
The output of this program is as follows:
Name is Shyam
Age is 30
Name is Johnny
Age is 3
The Identity class has three methods, The first is the constructor method which initializes the two instance variables of the class based on the arguments to new, The Identity class also includes a method called printIdentity() so that the object can 'introduce' itself and a main() method to test each of these things.
Calling another constructor
Some constructors may be supersets of other constructors defined within a class. This implies that the constructor might have the same behaviour as the other constructor plus other additional behaviour. Rather than duplicating identical behaviour in multiple constructors in a class, it is recommended to just call the first constructor from inside the body of the second constructor. Java provides a special syntax for doing this. A constructor defined within the current class can be called using the keyword this. The syntax would be as follows:
this(arg1, arg2, arg3,..);
The arguments to this() are the arguments to the constructor.
Similar to methods, constructors can take many different numbers and types of parameters in the body of one program, enabling the programmer to create objects with desired functionality.
Program shows the example of a class Rectangle that has overloaded constructors.
Using overloaded constructors.
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
Rectangle(int x1, int y1, int x2, int y2)
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
Rectangle(Point top Left. Point bottomRight)
x1 = topLeft.x;
y1 = topLeft.y;
x2 = bottomRight.x;
y2 = bottomRight.y;
Rectangle(Point top Left. int w. int h)
x1 = topLeft.x;
y1 = topLeft.y;
x2 = (x1 + w);
y2 = (y1 + h);
System.out.print("Rectangle: <" + x1 + “ , “+ y1);
System.out.println("," + x2 + "," + y2 + ">");
public static void main(String args[ ])
System.out.println("Calliing Rectangle with coordinates 5,5,25,25:");
rect = new Rectangle(5. 5, 25. 25);
System.out.println("Calling Rectangle with points (10,10). (20,20):");
rect = new Rectangle(new Point(10,10), new Point(20.20));
System.out.print("Calling Rectangle with point (10,10)");
System.out.println(" width (50) and height (50):");
rect = new Rectangle(new Point(1 0.1 0). 50, 50);
System.out.printIn(" ---------- ") ;
The output of Program is the following:
Calling Rectangle with coordinates 5,5,25,25:
MyRect: <5, 5, 25, 25>
Calling Rectangle with points (10,10), (20,20):
Calling Rectangle with point (10,10), width (50) and height (50):
MyRect: < 10, 10,60, 60>