I am trying to initialize Generic Parameter and Array
like
import java.util.ArrayList;
import java.util.List;
public class GenericExample<T> {
/*
* Complier error -->
*
* Cannot instantiate the type T
*/
private T t = new T();
/*
* Complier error -->
*
* Cannot create a generic array of T
*/
private T[] tArray = new T[10];
/*
* No Complier error.
*/
private List<T> list = new ArrayList<T>();
}
There is no error when initialize list with Generic type as follows:
/*
* No Complier error.
*/
private List<T> list = new ArrayList<T>();
However, Complier errors appear when I am using the following initialization.
/*
* Complier error -->
*
* Cannot instantiate the type T
*/
private T t = new T();
/*
* Complier error -->
*
* Cannot create a generic array of T
*/
private T[] tArray = new T[10];
Can someone help me out with the following 2 questions:
Q1: Why List<T> list = new ArrayList<T>(); does not encounter Complier error?
Q2: Why Complier error is thrown when using private T t = new T(); and private T[] tArray = new T[10]; ?