Java Chaining Constructors Tutorial

In this section, we will learn what the constructor chaining is and how to use it in Java.

Note: we’re assuming you’re familiar with the constructors in Java.

What is chaining constructors in Java?

The process of creating multiple constructors in Java and allowing one constructor to call another one in the same class is called chaining constructors.

Basically, constructors can hand over the process of constructing an object to each other.

Why use chaining constructors in Java?

If you think about it, when creating multiple constructors, we need to copy and paste the same logic in the body of all the constructors. This is basically a waste of time and resource. Also if in the future we needed to make some change in the instructions. Not just one constructor, but the entire constructor’s body should be modified!

For this reason, we can simply design and put the logic in to one constructor only, and the other constructors simply hand over the constructions to that fully defined constructor.

How constructor chaining is implemented in Java?

  • Using `this()` keyword
  • Using `super()` keyword

Constructor chaining via `this()` keyword in Java:

Using the `this` keyword, we can link constructors of a class in a way so that when one constructor is called, it passes the execution to another constructor.

Example: using constructor chaining via `this()` keyword in Java

Let’s say we have a class named Employee with this body:

public class Employee {
    private String name;
    private String lastName;
    private int id;

    public Employee(){}
    public Employee(int id){
        this.id = id;
    }
    public Employee(String name, String lastName){
        this.name = name;
        this.lastName = lastName;
    }
    public Employee(int id, String name, String lastName){
        this.id = id;
        this.name = name;
        this.lastName = lastName;
    }

}

Here we have 4 constructors and each is independent of the other. Basically, none of them call the other in this class.

Now let’s refactor the constructors of the Employee class and apply chaining constructors:

public Employee(){
this(1, "John","Doe");
}
    public Employee(int id){
        this(id, "John","Doe");
    }
    public Employee(String name, String lastName){
        this(1,"John","Doe");
    }
    public Employee(int id, String name, String lastName){
        this.id = id;
        this.name = name;
        this.lastName = lastName;
    }

Within the body of the first three constructors, we use the keyword `this` and followed by that we used the parentheses with 3 arguments.

The keyword `this` refers to the object that is going to be created. So when we used it in the constructors, it means: create an object from the Employee class but use the constructor that accepts 3 arguments. This is because every time we called the `this` keyword inside the constructors, we put 3 arguments.

If we used 2 or 1 argument when calling the `this` keyword, the compiler would use the constructor of the Employee class that uses 2 or 1 argument to construct an object.

Note: The use of chained constructors is mostly for cases where we have default values for a few member variables of the class and in cases where those values were not explicitly defined by the user, we want to use these default values. In such cases we don’t need to set instructions in the body for all the constructors of the class! Instead, we can fully define the body of one constructor only and for other constructors we just redirect the constructing process to the fully defined constructor.

Example:

public class Employee {
    private String name;
    private String lastName;
    private int id;

    public Employee(){
        this(1, "John","Doe");
    }
    public Employee(int id){
        this(id, "John","Doe");
    }
    public Employee(String name, String lastName){
        this(1,"John","Doe");
    }
    public Employee(int id, String name, String lastName){
        this.id = id;
        this.name = name;
        this.lastName = lastName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

}

Now inside the `Simple` class:

 

public class Simple {

    public static void main(String[] args) {
        Employee emp = new Employee(20);
        System.out.println(emp.getName());
        System.out.println(emp.getLastName());
    }
}

Output: John Doe

In this example we’ve created an object from Employee class and because we used only one integer value as the argument of the constructor then the ` public Employee(int id) ` is selected to construct the object.

Inside the body of this constructor we have:

public Employee(int id){
    this(id, "John","Doe");
}

And as you can see we’ve called another constructor of the Employee class that takes 3 arguments. The value of the `id` is taken from the user but the other two values are set by default.

Now the constructor that takes 3 arguments will run and construct the object.

To be more specific, this is the constructor that we’re talking about:

public Employee(int id, String name, String lastName){
    this.id = id;
    this.name = name;
    this.lastName = lastName;
}

Constructor Chaining to other class using super() keyword in Java:

Because the `super` keyword is mainly related to the topic of the inheritance and we didn’t touch this topic yet, the explanation of this part will be deferred until the Java super() section.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies