5

I was writing something using generics and to my surprise I found that this doesn't work:

class foo<T>{

 T innerT = new T();

}

So can't I instantiate the genericized type? Aren't there any ways to do this?

0

6 Answers 6

14

At the runtime JVM doesn't know what T is so it can't create new object of type T. Types are erased during compilation.

3
  • +1. Type erasure and what Generics really means in Java is a must.
    – Joe
    Jan 22, 2010 at 10:02
  • 11
    Furthermore, T may be an interface or an abstract class.
    – Jerome
    Jan 22, 2010 at 10:02
  • 3
    Or an enum, or just an ordinary concrete class with no accessible no-args constructor. Jan 22, 2010 at 12:54
9

Yeah, it's pretty damn annoying.

The work around I use is to force the client to pass the class in when constructing a new foo<T> - i.e.

public foo(Class<T> myClass)

Then you can use myClass.newInstance() instead.

5
  • can't be less arsed... thanks man didn't think about this :D
    – gotch4
    Jan 22, 2010 at 10:07
  • 2
    This answer doesn't explain why. You should understand why it does this before working round it.
    – Joe
    Jan 22, 2010 at 10:19
  • 4
    And T can be abstract class or interface, in which case the solution is absolutely not useful.
    – nanda
    Jan 22, 2010 at 10:30
  • Other answers have explained the reason for type erasure quite well. I am happy to share to the rep without basically copying what others have said.
    – cyborg
    Jan 22, 2010 at 10:45
  • Although after years I understand type erasure, still there is no need for that. In fact genericized instantiation will be done at compile time, so if T is an interface/abstract class/class without a no-argument constructor, the error can be resolved by the compiler. Type erasure is a runtime problem, not a compile time one.
    – gotch4
    Apr 8, 2015 at 9:50
8

I would always prefer a creational pattern (factory, prototype, ...). Especially

public foo(Class<T> myClass)

Then you can use myClass.newInstance() instead.

is a bad solution, because T may have no empty constructor.

2

The trick here is to "wrap" the constructor in its own object, and then use that object to create the class you need.

If you dont know what class you are instancing, well then you dont know if it has a constructor, thus you cant call it.

static <T> void myFunction(Provider<T> provider){
    T t = provider.get();
}

interface Provider<T>{
    public T get();
}

public class MyClass{
}

public static void main(String [] args){
    myFunction(new Provider<MyClass>(){
        public MyClass get(){
            return new MyClass();
        }
    });
}
1

This is a limitation in the way java implemented generics. To allow compatibility of code using generics with legacy code before generics where added to the language, generics were implemented using type erasure. This causes a lot of confusion to people who come from langauges such as C++ and expect generic's to work in the same way.

To read more on the implications of this here is a good link

1
  • 2
    In fact I'm a bloody C++ programmer and I always get involved in projects with this f...g Java language...
    – gotch4
    Jan 22, 2010 at 10:16
0

Workaround

You can use the template type of a class if you subtype it. See my answer to another question - Instantiating generics type in java.

Not the answer you're looking for? Browse other questions tagged or ask your own question.