According to the question, Create instance of generic type in Java?
At the time of writing ,the best answer is found to be...
private static class SomeContainer<E> {
E createContents(Class<E> clazz) {
return clazz.newInstance();
}
}
But the answer works only for this SomeContainer.createContents("hello");
My condition is to put the class as an inner class in a generic class,then the code should be like this.
SomeContainer<T>.createContents(T);
That will produce compile time error. There is not any created references for T
,also. Is there any way to create completly new T
object inside the generic class?
Thanks in advance