As always, nested non- private types do not help keep things simple. Pull out the boat and create new files, even if your IDe is working against you.
In any case, the static members of the class do not share the general parameters of the outer class. This is important for field behavior and provides great flexibility for static nested types (like InnerInterface ), even if you don't want to. Thus, in your case, you need to provide your (static) nested interface with a common parameter. For $ deity, use a different identifier!
public class OuterClass<E> { public class InnerClass { public E someMethod() { return null; } } public interface InnerInterface<T> { void onEvent(OuterClass<T>.InnerClass innerClass); } } public class Test { public static void main(String[] args) { OuterClass<Integer> o1 = new OuterClass<Integer>(); OuterClass<String> o2 = new OuterClass<String>(); OuterClass.InnerInterface<Integer> innerInterface = new OuterClass.InnerInterface<Integer>() { @Override public void onEvent(OuterClass<Integer>.InnerClass innerClass) { Integer i = innerClass.someMethod(); } }; } }
(I also qualified InnerClass , if necessary.)
Edit: maybe you should just take a look at the original anonymous inner class in isolation from the rest of the use:
public interface InnerInterface{ public void onEvent(OuterClass.InnerClass innerClass); } OuterClass.InnerInterface innerInterface = new OuterClass.InnerInterface() { @Override public void onEvent(InnerClass innerClass) { Integer i = innerClass.someMethod(); } };
Integer appears only once, in a place where it clearly cannot be considered correct.
Tom Hawtin - tackline
source share