What is the explanation for the following:
public class GenericsTest { //statement 1 public ArrayList<Integer>[] lists; public GenericsTest() { //statement 2 lists = new ArrayList<Integer>[4]; } }
The compiler accepts statement 1. Statement 2 is marked by the compiler to โcreate a shared arrayโ.
A good explanation that I saw regarding the rejection of shared arrays is this one , arguing that since arrays are covariant and generics are you cannot undermine typical typing if you have allowed shared arrays.
Leaving aside the argument about whether the language should go to the extreme stages of creating such a complex inconsistency in the treatment of generics so that you do not shoot yourself no matter how hard you try (and if anyone knows about good discussions about the relative merits / faults of the problem, please write to me, I would be interested to see the arguments), why do I need to state (1) if (2) is not?
java arrays generics
Steve B. Jan 22 '09 at 17:58 2009-01-22 17:58
source share