Community
Participate
Working Groups
Build Identifier: M20100211-1343 The following code compiles cleanly in javac and shows an error in Eclipse. class A { private int x; static class B { private int x; private C c = new C() { void foo() { x = 3; // error: "The field x is defined in an inherited type and an enclosing scope" } }; } static class C { private int x; } } See JLS 8.1.6, last two paragraphs, and JLS 8.2 "Members of a class that are declared private are not inherited by subclasses of that class". C.x is not in scope in the anonymous subclass of C and the only possible meaning of the name x is B.x. Interesting fact #1: if C.x is changed to public or protected the compiler correctly shadows B.x with C.x and the error goes away. Interesting fact #2: the following variation with the anonymous class pulled one level up compiles correctly: class A { private int x; private C c = new C() { void foo() { x = 3; } }; static class C { private int x; } } Reproducible: Always Steps to Reproduce: Paste the snippet above into a Java editor.
Created attachment 172705 [details] junit from submitter's report. This codifies the current behavior and will have to change suitably as the source code changes.
Created attachment 173410 [details] Patch under consideration
All tests pass. Jay, please review. Thanks in advance.
Patch looks good to me. One observation, though. When I changed the specifier to public in the super class as in the example below, I get the compiler error as mentioned in the comment #0. However, this error doesn't appear if the compiler level is set to >= 1.4. class A { static class B { private int x; public C c = new C() { void foo() { x = 3; // Error only when compiler is set to 1.3 } }; } static class C { public int x; } } Looking at the code, this might be the expected behavior after all. One concern, though, is that javac doesn't report any error at all no matter what -source I set.
(In reply to comment #4) > Patch looks good to me. > > One observation, though. When I changed the specifier to public in the super > class as in the example below, I get the compiler error as mentioned in the > comment #0. However, this error doesn't appear if the compiler level is set to > >= 1.4. [...] > Looking at the code, this might be the expected behavior after all. Yes, Please see http://dev.eclipse.org/mhonarc/lists/jdt-core-dev/msg00071.html (The JLS sections mentioned there is not w.r.t JLS3). So this is a deliberate choice. See also: https://bugs.eclipse.org/bugs/show_bug.cgi?id=3350 and https://bugs.eclipse.org/bugs/show_bug.cgi?id=3361
Created attachment 174113 [details] Same patch after CVS synchronization.
Released in HEAD for 3.7 M1.
Verified for 3.7M1.