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.

2
  • 1
    No, there is not. The type parameter of any generic is removed at run time. Look up Runtime Type Erasure for more information. Commented Sep 4, 2013 at 6:42
  • 1
    stackoverflow.com/questions/1004022/… Commented Sep 4, 2013 at 6:43

4 Answers 4

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())) {
        //...
       }
    }

}
Sign up to request clarification or add additional context in comments.

3 Comments

I didn't do that downvote. Thanks for your suggestion. Let me try that now.
@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.
This is dangerous, e.g. you can get Integer as type parameter class, but it was actually instantiated as Node<Number>.
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) {
      //...
    }
  }

}

Comments

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;
    }
    //.....    
}

Comments

-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();
    }       

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.