Acces to private field from nested class when the type of reference is a generic type variable with upper bound of outer class
David M. Lloyd
david.lloyd at redhat.com
Fri Oct 7 13:13:10 PDT 2011
But the field _would_ be accessible according to JLS §6.6.1 regardless
of whether the object was of a subtype. It seems pretty clear to me,
but maybe I'm just being dense?
On 10/07/2011 02:43 PM, maurizio cimadamore wrote:
> Yep - that's intentional - it's listed in the compatibility notes of the
> JDK 7 release [1].
> In general accessing private members of a type-variable is not sound
> (note that O could be instantiate by a subclass of Outer in which the
> field would not be accessible).
>
> [1] -
> http://www.oracle.com/technetwork/java/javase/compatibility-417013.html#source
>
>
> Maurizio
>
>
> On 07/10/2011 20:29, Peter Levart wrote:
>> 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
>
--
- DML
More information about the compiler-dev
mailing list