-1

So I want to create a class that in turn instantiates an instance variable as below. My problem is what do I replace Example.class with to make it generic T.class doesn't work and neither does Class.

public class MyClass<T> {
   public CurstomClass<T> customClass = new CustomClass<T>(Example.class); 
}
Fredrik L
  • 1,790
  • 4
  • 21
  • 28
  • possible duplicate of [Getting T.class despite Java's type-erasure](http://stackoverflow.com/questions/2225979/getting-t-class-despite-javas-type-erasure). See also: [how to get class instance of generics type T](http://stackoverflow.com/questions/3437897/how-to-get-class-instance-of-generics-type-t) – Paul Bellora Aug 06 '13 at 23:19

1 Answers1

0

There is only one way to do it in Java - pass generic type class as a parameter:

 public class MyClass<T> {

    public final CustomClass<T> customClass;

    public MyClass( Class<T> clazz) {
      customClass = new CustomClass<T>(clazz);   
    }

 }
Eugene Ryzhikov
  • 17,131
  • 3
  • 38
  • 60