Other |
---|
1.4.2 mantisFixed |
Duplicate :
|
|
Duplicate :
|
|
Duplicate :
|
|
Relates :
|
This is a correction to 4531312: As pointed out by the Java Spec Report, http://www.ergnosis.com/java-spec-report/java-language/jls-8.8.5.1-d.html, it is legal to reference enclosing types in an explicit constructor invocation. For example, this compiles successfully with javac: $ cat C.java class C { C(Object o) {} private class Middle extends C { Middle(int i) { super(null); } Middle() { super(C.this.new Middle(1).new Inner(){}); } class Inner {} } } $ javac C.java $ However, remove the reference to C.this, and the compiler throws up its hands. $ cat C.java class C { C(Object o) {} private class Middle extends C { Middle(int i) { super(null); } Middle() { super(/*C.this.*/new Middle(1).new Inner(){}); // only this line changed } class Inner {} } } $ javac C.java C.java:8: cannot reference this before supertype constructor has been called super(/*C.this.*/new Middle(1).new Inner(){}); // only this line changed ^ 1 error $ This goes against JLS 15.9.2: Middle is an inner member class, the class instance creation expression is unqualified, so bullet 3.1.2 applies: the innermost class O which contains Middle as a member is C, so the enclosing instance i should be C.this, and the example should compile identically to the previous example.
|