Community
Participate
Working Groups
I20040427 The following test case has a compile error on the reference to "I.this" inside the anonymous implementation of I. It says there is no enclosing instance of type I. This is not true, since the anonymous type itself is of type I. An unqualified "this" reference compiles fine. public class A { interface I { public void doit(); } public void foo(I i) { } public void bar() { new I() { public void doit() { foo(I.this); } }; } }
I renamed the class X, and I compiled it using javac 1.4.2 and jikes 1.18. Both also report an error in this case: javac 1.4.2: X.java:10: not an enclosing class: X.I foo(I.this); ^ 1 error jikes 1.18: Found 1 semantic error compiling "C:/tests_sources/X.java": 10. foo(I.this); ^ *** Semantic Error: Interface "X$I" cannot be used where a class is expected.
Feel free to close if you think the current behaviour is correct. It just seemed wrong to me.
The message is a little cryptic, however the rule is that you cannot qualify an enclosing type with a supertype. It has to match exactly one of the enclosing type name. Given it is an anonymous. this and A.this would be legite. I is only a supertype of the anonymous type.