Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Constructor Chaining in Java

DZone's Guide to

Constructor Chaining in Java

Invoking constructors from one another can be frustrating. This guide will walk you through successfully chaining constructors while avoiding compiler errors.

· Java Zone ·
Free Resource

How do you break a Monolith into Microservices at Scale? This ebook shows strategies and techniques for building scalable and resilient microservices.

Constructors are one of the simplest, yet trickiest object creation concepts in Java. A lot of experienced Java developers get confused about it. A source of confusion is relating it to methods, which is a totally different concept. One of the key differences between a method and a constructor is that constructors don't return anything. A lot of things happen under the hood when you invoke a constructor.

How Does Constructor Chaining Work?

When you invoke a constructor from another constructor, it's called constructor chaining. Whether you're dealing with a standalone constructor or a parent class' constructor, the end of the chain will always be the Object's class constructor because every class is inherited from the Object class by default.

So lets' look at the below scenario to understand it better:

class Parent {

    public void Parent() {
        System.out.println("inside parent without arguments");
    }

    public Parent(int a) {
        System.out.println("inside parent with argument");
    }
}

class Child extends Parent {

    public void Child() {
        System.out.println("inside child without arguments");
    }

    public Child(int b) {
        System.out.println("inside child with argument");
    }

    public static void main(String[] args) {
        Child child = new Child();
        Child child1 = new Child(15);
    }
}


So what should the output be? It will give a compilation error. 

1. Line 18 will give a compilation error saying that there is no default constructor in the Parent class. Huh?

Doesn't the compiler add a default constructor by itself? No.

Let's understand what the compiler is trying to do here: 

  • The compiler doesn't add a default constructor (no args) if you have already defined a constructor with arguments. 

  • The compiler tries to add a super() call as the first call in the constructor if you don't add it yourself.

2. Line 23 will give compiler error because there is no default constructor defined in Child.

So how can we fix it?

1. We can fix line 18 in the following two ways:

  • Add a super(b), super call with an argument to the Parent class in the constructor of the Child class.

  • Add a default constructor in the Parent class.

2. We can fix line 23 by adding a default constructor in the Child class:

class Parent {

    // this is method not a constructor
    public void Parent() {
        System.out.println("inside parent without arguments");
    }

    // this is default constructor.
    public Parent() {
        super(); // hidden call to Object's default constructor
        System.out.println("inside parent's default constructor");
    }

    public Parent(int a) {
        super(); // hidden call to Object's default constructor
        System.out.println("inside parent's argument constructor");
    }
}

class Child extends Parent {
    // this is a method
    public void Child() {
        System.out.println("inside child without arguments");
    }

    // this is a default constructor
    public Child() {
        super(); // hidden call to Parent's default constructor
        System.out.println("inside child's default constructor");
    }

    public Child(int b) {
        super(b); // Case - 1 : call to Parent's argument constructor
        System.out.println("inside child's argument constructor");
    }

    public static void main(String[] args) {
        Child child = new Child();
        Child child1 = new Child(15);
    }
}


Output : 

inside parents's default constructor

inside child's default constructor

inside parent's argument constructor

inside child's argument constructor


What Is this() Used For?

If you need to call the same class' constructor, then you can use this() as the first statement of the constructor. It can be with or without arguments based on which constructor you want to invoke.

class Child extends Parent {
    // this is a method
    public void Child() {
        System.out.println("inside child without arguments");
    }

    // this is a default constructor
    public Child() {
        this(); //can we call this here?
        System.out.println("inside child's default constructor");
    }

    public Child(int b) {
        this(); // invoke default constructor of Child class
        System.out.println("inside child's argument constructor");
    }

    public static void main(String[] args) {
        Child child = new Child(15);
    }
}


Output: 

Line 9, will give a compilation error. It is a recursive call to the default constructor. However, it is legitimate to call it on line 14 because it is being called from an argument constructor. If we remove line 9, then the output will be :


inside parent's default constructor 
(the compiler will add a call to super() in the default constructor of the Child class, which will invoke Parent's default constructor)

inside child's default constructor

inside child's argument constructor


I hope the concept of constructor chaining is clear now!

How do you break a Monolith into Microservices at Scale? This ebook shows strategies and techniques for building scalable and resilient microservices.

Topics:
java ,constructors ,object creation in java ,constructor chaining ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}