How to use the parent parent type parameter inside local classes?
Why can't I refer to the type parameter of the parent parent class inside local local classes?
public class IsGeneric<T> {
public void doSomething(T arg) {
class A {
T x;
}
A foo = new A();
foo.x = arg;
T bar = foo.x; // error: found java.lang.Object, required T
}
}
According to Eclipse, the above code works fine, but javac 1.6.0_11 seems to be of foo.x
type java.lang.Object. A workaround to the problem is obviously to make the A
generic itself, for example in the following code:
public class IsGeneric<T> {
public void doSomething(T arg) {
class A<S> {
S x;
}
A<T> foo = new A<T>();
foo.x = arg;
T bar = foo.x;
}
}
However, I would like to understand what is wrong with the first option. Any ideas?
+2
source to share
1 answer
This may be a bug on Sun javac
, see this question which includes possible solutions in the answers.
+1
source to share