Can you use this() and super() both in a constructor?

Solution:

No, because this() and super() must be the first statement in the class constructor.
Example:

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

0 answers