I know i can create an instance of a Type Paramter using the method given here. But why doesnt the following statement compile?
public static <E> void append(List<E> list) {
E elem = new E(); // compile-time error
list.add(elem);
}
List<String>list=new ArrayList<String>();
append(list);
1.Cant the type of E be detected based on what i pass in the method append()? Then what is Type Inference used for?
2. Even if Type Inference is not applied, then wont E elem=new E()
will b converted into Object elem=new Object()
at run time, which should be fine? In any case, it should work. But why it doesnt?
EDIT: The duplicate qeustion that they have given link to only answers the methods by which we can create an instance of Type Parameter. It doesnt answer why this method wont work.