3

What is the best way to retrieve the runtime value of a generic parameter for a generic class? For example:

public class MyClass<T> {

  public void printT() {
    // print the class of T, something like:
    // System.out.println(T.class.getName());
  }

}

So if I call

new MyClass<String>().printT() 

it will print "String"

1
  • Why does it matter ? If it really matters dont generify use the actual Type. Commented Oct 15, 2009 at 9:39

4 Answers 4

6

You don't. Due to type erasure that information is (mostly) lost at runtime. If you really need the class this is what you do:

public class MyClass<T> {
  private final Class<T> clazz;

  public MyClass(Class<T> c) {
    if (c == null) {
      throw new NullPointerException("class cannot be null");
    }
    clazz = c;
  }

  public void printT() {
    System.out.println(clazz.getName());
  }
}

and then you have access to it.

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

1 Comment

Note: Your example code contains some errors, like private final clazz; - you forgot the type of the variable.
1

To achieve that, you need to add the type info, since type erasure means that T's type is not available.

public class MyClass<T> {
  private final Class<T> clazz;
  public MyClass(Class<T> clazz) {
    this.clazz=clazz;
  }

  public void printT() {
    // print the class of T, something like:
    System.out.println(this.clazz);
  }
}

Comments

1

Java doesn't have that information at runtime, because of type erasure. You would need to pass the type as a constructor parameter to the class when the object is instantiated. There are some libraries and languages which can help you to do that with less typing: Guice can do it and also Scala can do it.

Comments

0

As mentioned before, you can't obtain type information at that example because of type erasure.

However, you can redesign your classes hierarchy in order to have a generic superclass/interface and make child classes directly define type parameter at their definitions:

package com;
import java.lang.reflect.ParameterizedType;
import java.util.Arrays;

public class AAA {
    public static void main(String[] args) throws Exception {
        Object target = new MyClass<Integer>() {}; // child class that explicitly defines superclass type parameter is declared here
        ParameterizedType type = (ParameterizedType) target.getClass().getGenericSuperclass();
        System.out.println(Arrays.toString(type.getActualTypeArguments()));
    }
}

class MyClass<T> {
}

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.