I needed to create an Array of a List of Strings (actual problem anonymized). With generics I declared the variable:
Then I proceeded to create it.
typedListArray = new List
; // compile error
I needed to create the array first, like how I create new int;. But no matter how I moved the syntax, it wouldn’t compile. Read the JLS and searched the web, and finally found this explanation. It was not even a solution.
The reason the creation was disallowed was due to type erasure. Java’s implementation of generics only applies at compile time to ensure that the correct types are used. During runtime, the type information is erased. By creating a concrete parameterized array type, the runtime is unable to ensure that the correct items are added to the list. It is therefore senseless to allow the creation. The ways “around” it were to live with the warning, or to suppress it with annotations.
Then why allow the declaration?