Is it possible to create an instance of a generic type in Java? I\'m thinking based on what I\'ve seen that the answer is no
(due to type erasure), but
If you want not to type class name twice during instantiation like in:
new SomeContainer(SomeType.class);
You can use factory method:
SomeContainer createContainer(Class class);
Like in:
public class Container {
public static Container create(Class c) {
return new Container(c);
}
Class c;
public Container(Class c) {
super();
this.c = c;
}
public E createInstance()
throws InstantiationException,
IllegalAccessException {
return c.newInstance();
}
}