I have a class defined like so:
public class Test {
static <T> List<Class<T>> filter(List<Class<T>> input) {
// code here
}
public static void main(String[] args) {
List<Class<? extends Throwable>> list =
new ArrayList<Class<? extends Throwable>>();
filter(list);
}
}
The filter method call in main gives the following compile error:
The method filter(List<Class<T>>) in the type Test is not applicable for the
arguments (List<Class<? extends Throwable>>)
I don’t understand why <T> doesn’t bind to <? extends Throwable>. Are there any generics gurus who can help me?
I believe the problem here is that there is no type for
?that would do.Consider if we, for simplicity, replaced
ClasswithAtomicReference.If the
inputconsisted of anAtomicReference<Integer>and anAtomicReference<String>we would be in trouble.The underlying problem that pointers to pointers in the presence of polymorphism is difficult. Without generics we can just hand wave. With generics we need to actually mean something.