this() Java : Calling constructor from constructor of same class


We have seen earlier constructor overloading with example. In constructor overloading, multiple constructors exist within the same class. Generally, to call multiple constructors, we create multiple objects; almost one object to call each constructor. Now the question is, Is there anyway to call all the constructors with one object creation? Yes, there is. The magic word is this() Java.

Note: this keyword very is different from this(). Notice, this refers the current object and this() is used to call one constructor from another; but condition is two constructors (calling and called) should belong to the same class (not of super of class; for super class super() comes).

Let us overload the constructor first and in the second program we will use this() and see the difference in the number of objects creation.

Simple overloaded constructors code without using this() Java.
public class Worker
{
  publicWorker()		                    // I
  {
    System.out.println("Hello 1");
  }
  publicWorker(String name)		            // II
  {
    System.out.println("Name is " + name);
  }
  publicWorker(String name, int age)	            // III
  {
    System.out.println("Name is " + name + " and age is " + age);
  }
  public static void main(String args[])
  {
    Worker worker1 = new Worker();       	    //  calls I
    Worker worker2 = new Worker("Mr.Rao");          //  calls II
    Worker worker3 = new Worker("Mr.Rao", 64);      //  calls III
  }
}

This program you are already aware of and know as method overloading. To call all the three constructors you created three objects of Worker class. Let us modify the code where with one object creation, all the three constructors are called. Just use this() Java here.

Following is the modified code of the previous with extra this() Java.
public class Worker
{
  public Worker()		                    // I
  {
    System.out.println("Hello 1");
  }

  public Worker(String name)		            // II
  {
    this();
    System.out.println("Name is " + name);
  }

  public Worker(String name, int age)		    // III
  {
    this("Raju");
    System.out.println("Name is " + name + " and age is " + age);
  }

  public static void main(String args[])
  {
    Worker worker3 = new Worker("Mr.Rao", 64);      //  calls III
  }
}

this() Java

In the above code, the same three constructors exist but extra some this() stuff. In the main(), just one object is created calling III (it can be any object of I or II also). From III constructor, this("Raju") is called. this("Raju") statement with parameter string matches the II constructor. Now the II is called. From II, we write this(). this() does not carry any parameter and thereby calls the default constructor I. So finally, III calls II and II calls I. Now all the three constructors are called with one object creation. But the output will be reverse order, that is I, II and III. Can you guess why? If not, send me query.

Important Rule: If exists, this() must be the first statement in the constructor. If you write as second or third, it is a compilation error. For this reason, we cannot have two this() statements in the same constructor (as both cannot be first). You can call one to one and the ladder can go to any number of constructors.

Finally what is learnt on this() Java:

  1. A constructor can be overloaded.
  2. Using this(), from constructor another can be called where both constructors belong to the same class.
  3. Call to this() must be the first statement in the constructor.
  4. We cannot have two this() statements in the same constructor.

Other Constructor related Topics

  1. Constructors and Constructor overloading

Leave a Comment

Your email address will not be published.