Here is a simple example of some code that compiles using Java 6, but does not compile in Java 7.
public class Test<T extends Test> {
private final int _myVar;
public Test(int myVar) {
_myVar = myVar;
}
public int get(TestContainer<T> container){
T t = container.get();
return t._myVar;
}
private static class TestContainer<T extends Test> {
private final T _test;
private TestContainer(T test) {
_test = test;
}
public T get(){
return _test;
}
}
}
In Java 7, it fails to compile in the get(TestContainer<T> container)
method, with the error:
error: _myVar has private access in Test
I don't understand why this no longer compiles - in my mind it should. The variable t
is of type T
, which must extend Test
. It's trying to access the field _myVar
of a instance of Test
from within the class Test
.
Indeed, if I change the method get(TestContainer<T> container)
to the following, it compiles (with no warnings):
public int get(TestContainer<T> container){
Test t = container.get();
return t._myVar;
}
- Why does this no longer compile?
- Was this a bug in Java 6? If so why?
- Is this a bug in Java 7?
I've had a google and searched in the Oracle bug database, but haven't found anything on this...