If I have a Class parameter, I can get the type of the parameter and
use it.
void method(Class<T> clazz)
{
Map<String, T> t = new HashMap<String, T>();
}
Is there any way to do the reverse, i.e. with a type variable, is there
a way
to get the Class ?
void <T> method(T t) {
Class<T> clazz = T.class; // does not work
Class<T> clazz = t.getClass(); // works, but only if t is not null
}
Thx
Jeroen V. - 18 Apr 2006 18:37 GMT
> Is there any way to do the reverse, i.e. with a type variable, is there
> a way
[quoted text clipped - 7 lines]
>
> Thx
The "class of a type variable" is a Class instance representing the
*runtime* (class)type of the variable. This means that null has no Class
since it has no runtime type. I don't think there is a way to get the
static type of a variable.