2

I have a small problem in java while using generics. I have a class A :

public class A<T>

In a method of A, I need to get the type name of T. Is there a way to find the string s using T ?

(If I create A<String> temp = new A<String>();, I want to be able to get java.lang.String at one point - I have to use generics because one of my methods will have to return a List).

This seems quite easy but I do not see how to do it.

Thanks a lot in advance for your help.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
yassiro
  • 23
  • 2

2 Answers2

3

Due to type erasure, this is not possible.

Instead, you need to accept a Type<T> in your constructor.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    Wow. I was *just* coding somethng with the same requirement. Got [this](http://stackoverflow.com/questions/2886414/programmatically-implementing-an-interface-that-combines-some-instances-of-the-s) which uses knowledge of a field in the class. – Miquel Dec 18 '11 at 15:12
  • (If I create A temp = new A();, I want to be able to get java.lang.String – yassiro Dec 18 '11 at 15:19
  • @yassiro: I know. However, that's not possible. Instead, you need to write `A temp = new A(String.class);` – SLaks Dec 18 '11 at 15:21
  • do tou try it !! System.out.println(x.getClass().getName()); it gives me the name of the generic class "A" – yassiro Dec 18 '11 at 15:30
  • @yassiro: Correct. You need to check the `Type` instance that was passed as a parameter. – SLaks Dec 18 '11 at 15:32
  • You don't know how to use a constructor parameter? You need to learn Java. – SLaks Dec 18 '11 at 15:39
  • You need to print the value of the constructor parameter. – SLaks Dec 18 '11 at 15:53
  • i cant understand you!! plz give me an exemple – yassiro Dec 18 '11 at 16:13
  • **i want to have a dynamic Jtable (this JTable can display many different Object "person","Student" )so i create a genereic TableModel then i want a name of the class T to get the fields of the class T!!! this my vision if someone need the code to understand what i mean, i"ll send it to him*** – yassiro Dec 18 '11 at 17:01
2

This is not possible, because generics are implemented using "erasure": there's actually just a single A class internally, and the <String> gets "erased" before run-time. The best solution, in my experience, is to add a private final field, Class<T> tClass, and to require it to be specified in the constructor:

public A(final Class<T> tClass)
{
    this.tClass = tClass;
}

You would then have to write

A<String> temp = new A<String>(String.class);

instead of just

A<String> temp = new A<String>();
ruakh
  • 175,680
  • 26
  • 273
  • 307
  • (If I create A temp = new A();, I want to be able to get java.lang.String – yassiro Dec 18 '11 at 15:19
  • @yassiro: Yes, I understand that. Unfortunately, you force me to quote the Rolling Stones: "You can't always get what you want / But if you try sometimes you might find / You get what you need." You *want* `new A()` to somehow record `String` at run-time; but you can't get that. So the question is, how to get what you *need*? – ruakh Dec 18 '11 at 15:25
  • **i want to have a dynamic Jtable (this JTable can display many different Object "person","Student" )so i create a genereic TableModel then i want a name of the class T to get the fields of the class T!!! this my vision if someone need the code to understand what i mean, i"ll send it to him*** – yassiro Dec 18 '11 at 17:01