Assuming T as a class type parameter, why cannot I use T.class
I was writing a function that download a page and parses it according to a passed class.
For parsing, I use another function whose signature is : ParseObject::parse(Class<T> classname)
<T> void downloadParse(){
ParseObject obj;
obj.parse(T.class); //<--- why compiler error here?? (whereas something like Integer.class is allowed)
}
Java generics are implemented via type erasure. They can only be used for compile time checking. After compiliation, the object gets changed to the lowest common object. (In this case Object.class).
The compiled bytecode has no idea what T is.
If you want access to the class, you need to change the method to: