I have an immutable class , with the following layout ,
public final class A <T> { private ArrayList<T> myList; private A(){ myList = new ArrayList<T>(); } public A<T> addData(T t){ A newOb = // make a new list and instantiate using overloaded constructor T newT = new T(); ***********ERROR HERE************* newOb.myList.add(newT); return newOb; } ......... } The error that I get here is cannot instantiate type T . Now , I think this is related to maybe type erasure in Java.
How can I overcome this ? I want to add a new copy of the argument that is being passed to addData into my list.
T newT = new T();toObject newT = new Object()to be available on runtime. But that also wrong sinceTcan be anythingextends Objectnot onlyObject. Hence compiler error.