I’d like to make a class that looks basically like this:
public class MyClass<T implements Serializable) {
void function() {
Class c = T.class;
}
}
Two errors:
– I cannot call T.class, even though I can do that with any other object type
– I cannot enforce that T implements Serializable in this way
How do I solve my two generics problems?
Cheers
Nik
You can’t get the type.
Generics are implemented using something called type-erasure.
The essence of this is that the type information is used by the compiler and discarded, hence not available at runtime.
With regards to the enforcing
T implements Serializable, you just need the following:This is simply referring to the is a relationship, so an class that implements
Serializable, is aSerializableand can be passed tofunction.