0

Consider the following code:

class MyClass<T>{
    public void method(){
        new T();//Compile error
    }
}

Does there exist a code example where type parameter can be matched to a specific type? This error causes because at the type checked at compile time compiler doesnt match type parameter T to a specific type. I know that we can apply reflection API to create instance of T.

St.Antario
  • 26,175
  • 41
  • 130
  • 318
  • Your client code knows about the actual type of `MyClass`. Why don't you create an instance of that type outside the constructor and pass it as an argument? – Sotirios Delimanolis Sep 28 '13 at 18:55
  • 1
    You can't create a new instance of a generic type. (Ever. Anywhere. Without using reflection, which I don't recommend). – Simon Forsberg Sep 28 '13 at 19:00
  • 1
    Please read [this question](http://stackoverflow.com/questions/75175/create-instance-of-generic-type-in-java) and I hope you will understand better. – Simon Forsberg Sep 28 '13 at 19:01

0 Answers0