actual type of variable type in parameterized type
Hi all,
Is there way to find out what is actual type of the Variable type in Parameterized type, for example:
class A<T> {
//actual type of variable type T
Class<T> tClass;
public void setTClass() {
// I don't know what should I do here
}
public Class<T> getTClass() {
return tClass;
}
public static void main(String[] args) {
assert((new A<Integer>().getTClass()).toString().equals("class java.lang.Integer"));
}
}
Message was edited by:
MjLaali
Message was edited by:
MjLaali
Message was edited by:
MjLaali
[663 byte] By [
MjLaalia] at [2007-10-3 4:48:47]

This is pretty mixed up stuff...
1) No, you can't directly find out the runtime type of a type parameter
2) You can achieve the same result using a technique called "type tokens" which is pretty much what you have already with your tClass member variable
3) You don't want the setTClass() method - people shouldn't be able to change this
4) You want a parameter of type Class<T> in your constructor:
...
public A(Class<T> clazz) {
this.tClass = clazz;
}
...
5) In your assert statement, don't futz about with strings, just do ...getTClass() == Integer.class
yes ofcource I can't chenge the actual type of variable type T in run time, but I would like to get actual class of T in run time like this example :
package test;
import java.lang.reflect.ParameterizedType;
abstract class B<T>{
private Class<T> variableClass;
public B() {
this.variableClass = (Class<T>) ((ParameterizedType) getClass()
.getGenericSuperclass()).getActualTypeArguments()[0];
}
public Class<T> getTClass(){
return variableClass;
}
}
class A extends B<Integer> {
public static void main(String[] args) {
assert(new A().getTClass() == Integer.class);
}
}
but I don't want class A and I would like to move main method to class B.
Dude, are you going to post this message to any more threads?In this case, you're wrong. This is nothing to do with self-typing. The OP just wants to know that the runtime type of T is.