Java constructors are special methods that are called when an object is instantiated. In other words, when you use the new keyword. The constructor initializes the newly created object. Here is a simple example that creates an object, which results in the class constructor being called:

EmployeeClass empClassObject 
            = new EmployeeClass();  

The above example results in a new EmployeeClass object being created, and also result in calling a special method (constructor) with zero arguments of EmployeeClass.

Defining a Constructor

Here is a simple Java constructor declaration example. The example shows a very simple Java class with a single constructor.


public  class EmployeeClass ()
{
   public EmployeeClass () {
  }
}

The constructor in the above code is

   
            public EmployeeClass () {
  }

In Java the constructor name has to be same name as the class. Also it is not required to declare a constructor in Java class. If you do not define any constructor in your class, java generates one for you by default. This constructor is known as default constructor.

Defining Multiple Constructors

A class can have multiple constructors, as long as their signature (the parameters they take) are not the same. You can define as many constructors as you need. When a Java class contains multiple constructors, we say that the constructor is overloaded (comes in multiple versions). This is what constructor overloading means, that a Java class contains multiple constructors.

Here is an e.g. of Java Class with multipe constructors


public class EmployeeClass{

	//Field 
	private String _firstName;
	private String _lastName; 	
	
	//Zero parameter constructor
	public EmployeeClass(){
		System.out.println("Zero parameter constructor has been called");

	}
	
	//2 parameter constructor
	public EmployeeClass(String fName , String lName ){
		System.out.println("2 parameter constructor has been called");
		
		//Initialize field values 
		this._firstName = fName;
		this._lastName = lName;
	}
	

  public static void main(String[] args) {
  	
   // Calling zero parameter constructor 
    EmployeeClass employeeObject1 = new EmployeeClass ();
    
    // Calling 2parameter constructor 
    EmployeeClass employeeObject2 = new EmployeeClass ("Sam" , "Harris");
    
  }
}

The Java class above contains two constructors. The first constructor is a no-arg constructor, meaning it takes no parameters. The second constructor takes two parameters. Inside the constructor body we initialize class fields _firstName and _lastName.

The output of the above program will be :

Zero parameter constructor has been called

2 parameter constructor has been called

Constructor Chaining

Constructor chaining is nothing but a scenario where in one constructor calls the constructor of its super class implicitly or explicitly. Suppose there is a class which inherits another class, in this case if you create the object of child class then first super class(or parent class) constructor will be invoked and then child class constructor will be invoked.

//Parent Class
public class Vehicle{
	public Vehicle () {
		System.out.println("Super class constructor has been called");
	}
}

public class Car extends Vehicle  {
	public Car () {
		System.out.println("Child class constructor has been called");
	}
	public static void main(String[] args) {
		Car car = new Car();
	}
}

Output :

Super class constructor has been called

Child class constructor has been called

We created instance of child class but Parent class constructor has been called first. The rule is when we create instance of child class parent class constructor called first and then the child class constructor.

  1. Every class has a constructor whether it’s normal one or a abstract class.
  2. Constructor are not methods and they don’t have any return type.
  3. Constructor name and class name should be the same.
  4. Constructor can use any access specifier, they can be declared as private also. Private constructors are possible in java but there scope is within the class only.
  5. Like constructors method can also have name same as class name, but still they have return type, though which we can identify them that they are methods not constructors.
  6. If you don’t define any constructor within the class, compiler will do it for you
    and it will create a constructor for you.
  7. this() and super() should be the first statement in the constructor code. If you don’t mention them, compiler does it for you accordingly.
  8. Constructor overloading is possible but overriding is not possible.
  9. Constructors can not be inherited.

6 thoughts on “What are Constructors?

  1. Love your articles. Not 100% sure use of zero parameter constructor in real life. Please share if possible.

Leave a Reply

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