Java – Constructor

In this Constructor tutorial, we will learn what is a constructor, why to use a constructor, and how to define a constructor in Java.

As a method in Java, the constructor is also a block of code but it does not have any return type. You need not call a constructor in Java because it is called automatically every time, whenever we create an object.

  • A Constructor should be public because it is called itself whenever an object is created. If we declare a private constructor then we will not be able to create an object outside the class.
  • In Java, whenever we run a program, the first step is object creation, after that constructor will be called automatically. So, we can use the constructor for initialization purposes, or Some important tasks that we want to execute very first.

Rules for Java Constructor

In java, you must follow some rules to create a constructor.

  1. The constructor must have the same name as the class name.
  2. It does not have any return type.
  3. A java constructor can’t be static or final.

Example: An example of a constructor in java.

class Example
{
	//creating a constructor
	public Example()
	{
		System.out.println("Constructor Example");
	}
public static void main(String args[])
{
	//constructor call itself, whenever object is created
	Example ex=new Example();
}
}
Output:
Constructor Example

If we create multiple objects, the constructor will be called multiple times.

Example e1=new Example();
Example e2=new Example();
Example e3=new Example();

It will print three times because we create an object three times.

Types of Constructor in Java

In Java, there are two types of constructors.

  1. Default Constructor
  2. Parameterized Constructor

1. Default Constructor

The constructor without parameter is known as the default constructor. It is also known as a no-arguments constructor.

class Example
{
	int a,b;
	//constructor with no-arguments
	public Example()
	{
		a=10;
		b=20;
	}
	public void sum()
	{
		System.out.println(a+b);
	}
public static void main(String args[])
{
	Example ex=new Example();
	ex.sum();
}
}

2. Parameterized Constructor

We can pass arguments to the constructor as input, it is referred to as a parameterized constructor.

class Example
 {
     int a,b;
     //constructor with arguments(parametrized)
     public Example(int x,int y)
     {
         a=x;
         b=y;
     }
     public void sum()
     {
         System.out.println(a+b);
     }
 public static void main(String args[])
 {
     //you need pass values to constructor
     Example ex=new Example(22,33);
     ex.sum();
 }
 }

Constructor Overloading

A class that can have more than one constructor with a different parameter is known as constructor overloading.

class Example
{
	int a,b;
	//constructor with no-arguments
	public Example()
	{
		a=1;
		b=1;
	}
	//constructor with arguments
	public Example(int x,int y)
	{
		a=x;
		b=y;
	}

	public void sum()
	{
		System.out.println(a+b);
	}
public static void main(String args[])
{
	Example e1=new Example(); //it will call default constructor
	Example e2=new Example(22,33); //it will call parametrized constructor

	e1.sum();
	e2.sum();
}
}

Copy Constructor

Sometimes in Java, you want to create a separate object but of the same copy of the previous object. In the copy constructor, a new object will create with values of the existing object of the same class.

class Rectangle
{
	int l,b;
	//constructor to initialize length & bredth
	public Rectangle(int x, int y)
	{
		l=x;
		b=y;
	}
	//copy constructor
	public Rectangle(Rectangle r)
	{
		l=r.l;
		b=r.b;
	}


	public void area()
	{
		System.out.println(l*b);
	}
public static void main(String args[])
{
	Rectangle r1=new Rectangle(10,15);
	Rectangle r2=new Rectangle(r1); //using copy constructor
	r1.area();
	r2.area();
}
}