Below I have a generic OuterClass, an InnerClass that use OuterClass generics and non-generic InnerInterface.
public class OuterClass<E> {
public class InnerClass {
public E someMethod() {
return null;
}
}
public interface InnerInterface{
public void onEvent(OuterClass.InnerClass innerClass);
}
}
In the main method below, I use two instance of OuterClass, o1 parameterized with , and o2 with . My annonymous inner class myListener tries to use the generic type of the outer class (E). The code as it is below does not compile (Integer i = innerClass.someMethod() - Type mismatch: cannot convert from Object to Integer).
public class Test {
public static void main(String[] args) {
OuterClass<Integer> o1 = new OuterClass<Integer>();
OuterClass<String> o2 = new OuterClass<String>();
OuterClass.InnerInterface innerInterface = new OuterClass.InnerInterface() {
@Override
public void onEvent(InnerClass innerClass) {
Integer i = innerClass.someMethod();
}
};
}
}
I would like to express that myListener is for o1, and should use E = Integer, without repeating it (without repeating , I already say it when declaring o1). Is that possible?
Many thanks! Faton.