Constructor in Java

In the previous section, we have discussed about the method in details. Now let’s discuss on Constructor in Java.

Objective:

  • What is Constructor
  • Default Constructor
  • Parameterized Constructor

What is Constructor?

Constructor is a block which is used to initialize the object through the new key word. The name of the Constructor is same as class name. This is can be considered as instance method, but this is not actually a method. Because constructor has no return type whereas method should have return type.

Example:

Rectangle rect = new Rectange();

From the above Rectangle is a class type, rect is an object, Rectangle() is Constructor and new is a keyword.

The Constructor is of the class Rectangle is called with the help of new key word. new keyword is used to create the object by calling the constructor of the same class automatically.  Let’s understand the constructor with a simple example.

Example:

class Construcotr_Demo{
	String tutorial_name;
	Construcotr_Demo ()
	{
		this.tutorial_name = "Core java";
	}
	public static void main(String args[]){
		Construcotr_Demo name = new Construcotr_Demo ();
		System.out.println("Tutorial name is " + name.tutorial_name);
	}
}

Output:

Tutorial name is Core java

Explanation:

As we seen in the example, Constructor_Demo is the class name and the constructor name. When it object is created with the new keyword then the Constructor_Demo constructor will be initialized and the Constructor inside the class will be called automatically. In this example the task of the constructor is to assign the tutorial_name value. Once it completes, the value of the tutorial_name is changed from NULL (default) to Core java.

When you will print the “name.tutorial_name” then it will print the value of the tutorial_name which is Core Java.

Now you are clear about the Constructor. What you will do if you want to mention different tutorial name with a single constructor ? 

Yes, you are correct, you need to use the parameterized Constructor. This is same as Parameterized method as we have done in our previous section. Let’s have a look on Parameterized Constructor with the same example (Calculating the area of rectangle.)

Example:

class Rectangle{
	double width; 
	double height; 
	Rectangle()
	{
		System.out.println("Constructor");
		width = 10;
		height = 19;
	}
	double area(){
		return width*height;
	}		
}

class RectangleDemo{
	public static void main(String args[]){
		Rectangle rect = new Rectangle();
		Rectangle rect2 = new Rectangle();
		double area_of_rect; 
		double area_of_rect2; 
		area_of_rect = rect.area();
		area_of_rect2 = rect2.area();
		System.out.println("Area of Rect is : " + area_of_rect + " & Arear of Rect2 is :  "+ area_of_rect2);
	}
}

Output:

Constructor
Constructor
Area of Rect is : 190.0 & Area of Rect2 is :  190.0
 

Explanation:

From the above example you can notice that rect and rect2 are initialized by Rectangle() Constructor when created. The constructor Rectangle given the 10 & 19 to width & height to the both all so the area is showing same in output.

You can notice that in output Constructor is showing twice then the output is showing. Do you know why?

This is the beauty of the Constructor. Whenever the object is created with e new keyword the constructor of the same class is executed. That is why Constructor is printed twice immediately then the area is calculating.

This is the example of the Simple constructor. Let’s perform the same task with the Parameterized Constructor.

Example : Parameterized Constructor

class Rectangle{
	double width; 
	double height; 
	Rectangle( double w, double h)
	{
		System.out.println("Constructor");
		width = w;
		height = h;
	}
	double area(){
		return width*height;
	}
}

class RectangleDemo{
	public static void main(String args[]){
		Rectangle rect = new Rectangle(20,22);
		Rectangle rect2 = new Rectangle(2,4);
		double area_of_rect; 
		double area_of_rect2; 
		area_of_rect = rect.area();
		area_of_rect2 = rect2.area();
		System.out.println("Area of Rect is : " + area_of_rect + " & Arear of Rect2 is :  "+ area_of_rect2);
		}
}

Output:

Constructor
Constructor
Area of Rect is : 440.0 & Arear of Rect2 is :  8.0

This is same as Parameterized method. You need to pass the parameter inside the constructor signature and provide the value while creating object in the constructor initialization with new keyword.

Example:

class Rectangle{
	double width; 
	double height; 
	Rectangle( double w, double h)
	{
		System.out.println("Constructor");
		width = w;
		height = h;
	}
	double area(){
		return width*height;
	}		
}

class RectangleDemo{
	public static void main(String args[]){
		Rectangle rect = new Rectangle(20,22);
		Rectangle rect2 = new Rectangle();
		double area_of_rect; 
		double area_of_rect2; 
		area_of_rect = rect.area();
		area_of_rect2 = rect2.area();
		System.out.println("Area of Rect is : " + area_of_rect + " & Arear of Rect2 is :  "+ area_of_rect2);
		}
}

Output:

Constructor in Java

This is the compilation error. As Rectangle rect2 = new Rectangle(); will search for default constructor but did not find. So we need to define the default constructor in this case.

Leave a Reply

Your email address will not be published. Required fields are marked *