// generic array creation in java: class cl1 {} class cl2 extends cl1 {} class clgen { void f() { //A[] x = new A[4]; // won't compile, generic array creation error. //var xx = new java.util.ArrayList[4]; //SAME ERROR! //clgen[] R = new clgen[5]; // SAME ERROR! clgen u; //u = new clgen(); //won't compile and shouldn't /* Can't mix invariant classes with covariant arrays at all. Suppose above line for R compiled, then since arrays are covariant, clgen[] R2 = new clgen[5]; // should also compile R2[0] = new clgen(); // R2 is a clgen object Object[] R3 = R2; // would compile because arrays are covariant // this violates the invariance of generics: u = (clgen)R3[0]; // the type of u is static clgen // but actual type of object is clgen. But all this is futile: */ clgen[] R2 = (clgen[]) new clgen[5]; //compiles with warning R2[0] = new clgen(); // compiles Object[] R3 = R2; // compiles because arrays are still covariant u = (clgen)R3[0]; // THIS COMPILES: u is assigned to a // clgen object, which again violates the static invariance of // of generic classes. // Java's answer is to not use arrays but ArrayList and other generic // classes. You're supposed to use arrays only for very small, isolated // purposes. But the lack of overloaded syntax on arrays make this // rather painful... } }//clgen