Inside a generic class Box<T>, which statement is illegal?
Correct answer B. T item = new T();
Explanation
Because of erasure, the actual type T is unknown at run time, so the compiler cannot generate a constructor call and new T() is a compile-time error. Declaring variables of type T, T[] or List is perfectly legal. A common workaround is to accept a Supplier or a Class and create objects through it.
Answer checked How we write and check questions
Discussion