A constructor of a class can be called from another constructor of the same class. The constructor calling statement (this()) should be the first statement in the constructor. Review the code below.
public class ClassA {
public ClassA(){
this(2);
System.out.println("Constructor without parameter.");
}
public ClassA(int a){
System.out.println("Constructor with int parameter: " + a);
}
public static void main(String[] str)
{
ClassA obj = new ClassA();
}
}
Output:
Constructor with int parameter: 2
Constructor without parameter.