No, you cannot use both this()
and super()
in the same constructor. This is because both keywords must be the first statement in the constructor, and there can only be one first statement. Therefore, you must choose one or the other.
The this()
keyword is used to call another constructor in the same class. This is useful when you have multiple constructors and you want to reuse code. The super()
keyword is used to call the constructor of the parent class. This is necessary to ensure that the parent class is properly initialized.
public class Test{
Test()
{
super();
this();
System.out.println("Test class object is created");
}
public static void main(String []args){
Test t = new Test();
}
}
Output
Test.java:5: error: call to this must be first statement in constructor