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
no
You are correct. You can't do new E(). But you can change it to
new E()
private static class SomeContainer { E createContents(Class clazz) { return clazz.newInstance(); } }
It's a pain. But it works. Wrapping it in the factory pattern makes it a little more tolerable.