I've read Oracle docs on Generics, and some reference books and i still cannot grasp some of the things about Java type erasing. First of all why aren't we allowed to say :
public class Gen<T> {
T obj = new T();
public T getObj() {
return obj;
}
public void setObj(T obj) {
this.obj = obj;
}
}
Why doesnt Java allow me to say new T()? I understand that memory allocation for object of type T is allocated at runtime and type erasure is done in compile time, but when the type erasure is done, all of my T's will be replaced with Objects, so why is this a big deal?
Also how is this type of manipulation with T[] possible : T[] arr = (T[]) new Object[size];
I just cant wrap my head around this things.
Thanks in advance.
I expected for it to create Object obj = new Object(), and to give me type safety throught the code, like inserting element, or extracting it with some getter. I dont understand why is this not allowed even with type erasure?