Is it possible to use a type variable declared by the enclosing class as a binding for a type variable declared in the inner class?
class Test<E> {
class Inner<T extends E> {}
<T extends E> void doStuff(T arg) {}
public static void main(String[] args) {
new Test<Number>().doStuff(new Integer(0));
new Test<Number>().new Inner<Integer>();
}
}
javac gives this error:
Test.java:6: type parameter java.lang.Integer is not within its bound
new Test<Number>().new Inner<Integer>();
^
I can not find a combination of types that will satisfy the compiler. What is the difference between a type parameter Tdeclared Innercompared to doStuff? Why does one work and the other not?
I'm not looking for alternatives, I just want to better understand how the language works.
source
share