Because of the erasure type , there is no way to do this: MyGenericObj<Integer>actually MyGenericObj<Object>behind the scene, regardless of its type parameter.
Class<T> , :
class MyGenericObject<T> {
private final Class<T> theClass;
public Class<T> getTypeArg() {
return theClass;
}
MyGenericObject(Class<T> theClass, ... the rest of constructor parameters) {
this.theClass = theClass;
... the rest of the constructor ...
}
}
getTypeArg, , Integer.class .