How can I get the \"real\" class of a generic type?
For Example:
public class MyClass {
public void method(){
//something
S
If you have a instance variable of type T in your class, and it happens to be set, then you could print the class of that variable.
public class Test {
T var;
public static void main(String[] args) {
Test a = new Test();
System.out.println(a.boo());
a.setVar(new Integer(10));
System.out.println(a.boo());
}
public String boo() {
if (var == null) {
return "Don't know yet";
}
return var.getClass().getSimpleName();
}
public void setVar(T var) {
this.var = var;
}
public T getVar() {
return var;
}
}