I want to use a generic class inside a nested static interface. My goal is to do something like this:
public class MyClass<T>{ private MyInterface task; public static interface MyInterface{ void aMethod (T item); } }
But I get the error: I canβt make a static reference to the non-static type T. If I make some changes (below), I can use the generic type inside the interface, but I want to avoid this method because it is redundant to write one class 2 times: one for MyClass and one for MyInterface.
public class MyClass<T>{ private MyInterface<T> task; public static interface MyInterface<T>{ void aMethod (T item); } }
Thanks.
EDIT : I want to do this:
MyClass c = new MyClass<String> (); c.setInterface (new MyClass.MyInterface (){ @Override public void aMethod (String s){ ... } );
or
MyClass c = new MyClass<AnotherClass> (); c.setInterface (new MyClass.MyInterface (){ @Override public void aMethod (AnotherClass s){ ... } );
java generics class interface
Gabriel llamas
source share