Constructor Calling in Java

Constructor calling in Java is possible in multiple ways. In this post, we’ll discuss each of those methods in detail. One thing we should remember is that constructor calling is only possible during the object creation. Once the object is created you won’t be able to call the constructor.

You can call a constructor in Java 3 different ways:

  1. Calling a constructor at the time of creating an instance of the class
  2. Calling an overloaded constructor within the class
  3. Calling a parent class constructor within the class

Calling constructor at the time of creating an instance of a class

In Java, to create an object we use new operator followed by the class name as below.

Employee emp = new Employee();

When we create an object like this, internally we are actually calling a no-arg constructor of the Employee class.

Sometimes, we also pass some arguments followed by the class name as below.

Employee emp = new Employee("E01", "John Doe");

In this case, we are calling a constructor with 2 String arguments while creating the object. Let us see a working example.

package com.techstackjournal;

class Employee {
	public Employee() {
		System.out.println("No-arg constructor");
	}
	
	public Employee(String empNo, String empName) {
		System.out.println("With arg constructor");
	}
}

public class ConstructorCalling {

	public static void main(String[] args) {
		
		Employee emp1 = new Employee();
		Employee emp2 = new Employee("E01","John Doe");
		
	}

}

Output of above program would be as follows:

No-arg constructor
With arg constructor

In this program, we are creating 2 instances, and each one is calling a one constructor. First object creation is calling an empty constructor. The second object creation is calling another constructor with 2 arguments.

Calling an overloaded constructor within the class

You can have more than 1 constructor, so that based on requirement you can call either of one. If you have already written the object initialization code within one constructor, you don’t want to repeat that in another constructor and you may want to call it in another constructor.

Java allows you to call a constructor in another constructor using this keyword. To call another constructor within the current constructor, all you need to do is, use this followed by the parameter list within brackets as follows:

this(arg1, arg2);

You need to make sure that this constructor calling using this keyword should be the first line within the constructor otherwise you would get a compilation error saying “Constructor call must be the first statement in a constructor” in eclipse. Let us look at an example.

package com.techstackjournal;

class Employee {
	
	private String employeeId;
	private String employeeName;
	private String email;
	
	public Employee() {
		System.out.println("No-arg constructor");
	}
	
	public Employee(String empNo, String empName) {
		this();
		this.employeeId = empNo;
		this.employeeName = empName;
		System.out.println("2 arg constructor");
	}

	public Employee(String empNo, String empName, String email) {
		this(empNo, empName);
		this.email = email;
		System.out.println("3 arg constructor");
	}
	
	@Override
	public String toString() {
		return this.employeeId + " " + this.employeeName + " " + this.email;
	}

}

public class ConstructorCalling {

	public static void main(String[] args) {
		
		Employee emp = new Employee("E01","John Doe", "john.doe@techstackjournal.com");
		System.out.println(emp);
		
	}

}

Output of the above program:

No-arg constructor
2 arg constructor
3 arg constructor
E01 John Doe john.doe@techstackjournal.com

In this example, you can see that we have a constructor which initializes employeeId and employeeName already in a 2 arg constructor. As we are writing another 3 arg constructor, we would like to reuse the 2-arg constructor code. So, we are calling it using this keyword followed by passing employeeId and employeeName. Similarly, 2-arg constructor is calling no-arg constructor without sending any parameters.

Calling parent class constructor within the class

A subclass derives or acquires member variables and member functions with Inheritance but not constructors. So, if you want to call a constructor which is written in parent class to reuse the code, you may call it using super keyword followed by a list of arguments.

super(arg1, arg2);

Again, you need to make sure this constructor call using super keyword is the first statement in your constructor.

package com.techstackjournal;

class Employee {
	
	private String employeeId;
	private String employeeName;
	
	public Employee() {
		System.out.println("No-arg constructor");
	}
	
	public Employee(String empNo, String empName) {
		this();
		this.employeeId = empNo;
		this.employeeName = empName;
		System.out.println("2 arg constructor");
	}


	@Override
	public String toString() {
		return this.employeeId + " " + this.employeeName;
	}

}

class Executive extends Employee {
	
	private String email;
	
	public Executive(String empNo, String empName, String email) {
		super(empNo, empName);
		this.email = email;
		System.out.println("3 arg constructor");
	}
	
	@Override
	public String toString() {
		return super.toString() + " " + this.email;
	}
}

public class ConstructorCalling {

	public static void main(String[] args) {
		
		Executive emp = new Executive("E01","John Doe", "john.doe@techstackjournal.com");
		System.out.println(emp);
		
	}

}

In this example, you can see that, within Executive class, we are calling the 2-arg constructor from the parent class Employee using super keyword.

Leave a Comment