Say I have these two methods:
<T extends MyClass> void method1(Class<T> a, ArrayList<T> b) {
//whatever
}
<T extends MyClass> void method2(T c) {
method1(c.getClass(), new ArrayList<T>()); //Here I get compile error: wrong argument type for the second parameter
}
While this other snippet returns no errors (only a warning for unchecked cast):
<T extends MyClass> void method1(Class<T> a, ArrayList<T> b) {
//whatever
}
<T extends MyClass> void method2(T c) {
method1((Class<T>)c.getClass(), new ArrayList<T>());
}
Can someone explain why I get the error in the first example, and what's the best way to deal with that?