How to call a constructor from another constructor within the same class (not from subclass)
Java
Invoking overloaded constructor from another constructor within the same class
stack overflow explained it well but let’s look at it more closely.
It is through the “this()” keyword.
class Temp
{
// default constructor 1
Temp()
{
System.out.println("default");
}
// parameterized constructor 2
Temp(int x)
{
// invokes default constructor
this();
System.out.println(x);
}
// parameterized constructor 3
Temp(int x, int y)
{
// invokes parameterized constructor 2
this(5);
System.out.println(x * y);
}
public static void main(String args[])
{
// invokes parameterized constructor 3
new Temp(8, 10);
}
}
Output is default -> 5 -> 80. Remember even though subclass constructor is invoked first, but it is the superclass constructor that is on top of Stack and thus is executed first. If you don’t remember, relook at previous post
Constructor Chaining to other class using super() keyword
class Base
{
String name;
// constructor 1
Base()
{
this("");
System.out.println("No-argument constructor of" +
" base class");
}
// constructor 2
Base(String name)
{
this.name = name;
System.out.println("Calling parameterized constructor"
+ " of base");
}
}
class Derived extends Base
{
// constructor 3
Derived()
{
System.out.println("No-argument constructor " +
"of derived");
}
// parameterized constructor 4
Derived(String name)
{
// invokes base class constructor 2
super(name);
System.out.println("Calling parameterized " +
"constructor of derived");
}
public static void main(String args[])
{
// calls parameterized constructor 4
Derived obj = new Derived("test");
// Calls No-argument constructor
// Derived obj = new Derived();
}
}
Output is Calling parameterized constructor of base -> Calling parameterized constructor of derived