I'm having trouble using generics. Given the following example :
class A<T> {
public A(Class<T> myType){
}
}
class B<E> extends A<Collection<E>> {
public B(Class<E> myEType){
super(???);
}
}
What ??? should be ?
Collection.class
don't work...
Collection<E>.class
neither.
(Class<Collection<E>>)Collection.class
do not work...
If there is a java generics guru, I need help... :/
You can't possibly get a Class<Collection<E>>
except for Collection.class
, because of type erasure. You'll have to use unsafe casts to cast a Collection.class
to a Class<Collection<E>>
-- specifically, (Class<Collection<E>>) (Class) Collection.class
will do the job.
class A<T> {
public A(Class<T> myType){
}
}
class B<E> extends A<Collection<E>> {
public B(Class<Collection<E>> myEType){ // <-- this is the changed line
super(myEType);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With