jeudi 26 février 2015

Confusion with explicit constructor invocation



In the code that I have provided the overloaded constructor with argument which makes a call to the no-args constructor, which inturn should call the constructor of the object class in this case. So how does using the this() keyword execute the program correctly but not a direct call to the constructor.



public class S {

S() {
System.out.println("S()");
}

S(int i) {

this();
S();// The method S is undefined for type S
System.out.println("S(int i)");
}

public static void main(String[] args) {
S obj1 = new S();
System.out.println("----------");
S obj2 = new S(10);
System.out.println("----------");

}

}



Aucun commentaire:

Enregistrer un commentaire