3

This is my class structure.

public class Node<T> {
    private T value;

    public Node(T val) {
        this.value = val;
    }

    public T evaluate() {
         return value;
    };

}

T can be Integer, Double, Date, Long or String.

Now, is there anyway I can get to know exactly what that type T is? Thanks.

E_net4
  • 27,810
  • 13
  • 101
  • 139
LPD
  • 2,833
  • 2
  • 29
  • 48

4 Answers4

7

You can invoke getClass() method on that generic variable and find out the class. Basically generics are erased at run-time, so an unbounded generic is nothing but java.lang.Object. So you can invoke all the method supported by Object class on that generic variable

At run-time, getClass() on generic object will return the actual class which was used to substitute the generic

For example

public class Node<T> {
    private T value;

    public Node(T val) {
        Class<?> clazz = val.getClass();
        checkType(clazz);
        this.value = val;
    }

    public T evaluate() {
         return value;
    };

    private void checkType(Class<?> c) {
       if(c.getName().equals(Integer.class.getName())) {
        //...
       }
    }

}
sanbhat
  • 17,522
  • 6
  • 48
  • 64
  • I didn't do that downvote. Thanks for your suggestion. Let me try that now. – LPD Sep 04 '13 at 06:51
  • @LPD The main point here being that if you have an actual instance of the class, you can identify it. If you do not have an instance there is no way. – le3th4x0rbot Sep 04 '13 at 07:38
  • 1
    This is dangerous, e.g. you can get `Integer` as type parameter class, but it was actually instantiated as `Node`. – Landei Sep 04 '13 at 09:12
1

Much easier, use instanceof.

public class Node<T> {
  private T value;

  public Node(T val) {
    checkType(val);
    this.value = val;
  }

  public T evaluate() {
     return value;
  };

  private void checkType(T val) {
    if(val instanceof Integer) {
      //...
    }
  }

}
Sky Kelsey
  • 19,192
  • 5
  • 36
  • 77
1

Though there is already an accepted answer, I think it may be good to add one more answer:

As mentioned by @BaileyS, there is no way to get info of T in a place without an instance of T.

The solution greatly depends on why you want T, how you are using it. Normally, if we need the type of T for the logic (e.g. creating a new instance of T), it is usually done by providing the Class instance.

For example, you can make your Node class be:

public class Node<T> {
    private T value;
    private Class<T> valueType;

    public Node(T val, Class<T> valueType) {
        this.value = val;
        this.valueType = valueType;
    }
    //.....    
}
Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
-1

If you know that your node will store only Number you could restrict it.

public class Node<T extends Number> {

    private final T value;

    public Node(T val) {
        this.value = val;
    }

    public T evaluate() {
         return value;
    };

    public Class<? extends Number> type() { 
      return value.getClass();
    }       

}