Acces to private field from nested class when the type of reference is a generic type variable with upper bound of outer class
Peter Levart
peter.levart at gmail.com
Fri Oct 7 12:29:38 PDT 2011
This code compiles with latest JDK 6 javac:
package test;
public class Outer
{
private int outerField;
public Outer(int outerField)
{
this.outerField = outerField;
}
public static class Inner<O extends Outer>
{
public int getOuterField(O outer)
{
return outer.outerField;
}
}
public static void main(String[] args)
{
Outer outer = new Outer(12);
Inner<Outer> inner = new Inner<Outer>();
System.out.println(inner.getOuterField(outer));
}
}
... but refuses to compile with JDK 7 javac (release or latest 7u2-b08 prerelease), giving the
following compilation failure:
src/test/Outer.java:16: error: outerField has private access in Outer
return outer.outerField;
^
even if tried with -source 1.6 -target 1.6 options.
Is this intentional or a bug?
Regards, Peter
More information about the compiler-dev
mailing list