Change in javac handling of anonymous/local classes

Alex Buckley alex.buckley at oracle.com
Wed Feb 27 13:16:30 PST 2013


On 2/27/2013 12:33 PM, John Spicer wrote:
> Javac 1.7 has what is a surprising difference from 1.6 with regard to this example:
>
> For example,
>
> abstract class TT {
>     void f1(String s) {}
>     void f1(int i1, int i2) {}
>     // Note: no f2
>     void f3(String s) {}
> }
> abstract class X1 {
>     void f1(int i) {}
>     void f2(int i) {}
>     void f3(int i) {}
>     void bar() {
>         new TT() {
>             {
>                 f1(0); // 1.6 error, 1.7 accepts if TT has > 1 f1, finds X1.f1
>                 f2(0); // 1.6 and 1.7 accept, find X1.f2
>                 f3(0); // 1.6 and 1.7 error, finds TT.f3
>             }
>         };
>     }
> }

Let's focus solely on 'f3(0);' for now, so we can be sure about the rules:

- The scope of the declaration of the method TT.f3(String) includes the 
body of the anonymous class declaration.

- The scope of the declaration of the method X1.f3(int) includes the 
body of the anonymous class declaration.

- The declaration of the method X1.f3(int) does not shadow anything. 
Nothing called f3 is declared in an enclosing scope at the point where 
X1.f3(int) is declared.

- Therefore, the declarations of both methods are visible at 'f3(0);'.

- Therefore, I would expect overload resolution to choose X1.f3(int).

- I suspect javac 1.6 and 1.7 choose TT.f3(String) because of a 
different belief about shadowing. Maurizio, please comment on what's 
visible at 'f3(0);'.

Alex



More information about the compiler-dev mailing list