What is the best way to retrieve the runtime value of a generic parameter for a generic class? For example:
public class MyClass<T> {
public void printT() {
// print the class of T, something like:
// System.out.println(T.class.getName());
}
}
So if I call
new MyClass<String>().printT()
it will print “String”
You don’t. Due to type erasure that information is (mostly) lost at runtime. If you really need the class this is what you do:
and then you have access to it.