tags:

views:

125

answers:

1

In code, if i write a code line like:

gClass<Double> x = new gClass<Double>();

And let say, that class is like:

public static class gClass<T> {
 private T value = null;
 public gClass() {
  // What is T? 
 }
}

Where i inserted the question: "What is T?", i don't want the value (what is null), but its type (what is instanceof Double).

+7  A: 

You can't find out T at execution time unless you add a constructor parameter to take Class<T> parameter and pass call it with Double.class in your case. You can then store that class and use it later.

This is due to type erasure... basically generics are just compiler magic to insert appropriate casts and check that things should be okay.

See the Type Erasure section of the excellent Java Generics FAQ.

You might also want to read Neal Gafter's blog posts about super type tokens and type literals.

Jon Skeet