JDK-7074966 : Allow the creation of Generic type Arrays
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 7
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_7
  • CPU: x86
  • Submitted: 2011-08-04
  • Updated: 2012-03-20
  • Resolved: 2011-08-04
Related Reports
Duplicate :  
Description
A DESCRIPTION OF THE REQUEST :
It is impossible to write programs/classes that implement generic type arrays.
For exemple, we can't make such a class:

public class myGenericClass<T> {
    private T[] genericArray;

    public myGenericClass (int length) {
        T = new T[length];    // genereic type arrays not allowed
    }
}

JUSTIFICATION :
It is really frustrating !

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I expect the following line of code to be valid:
T = new T[length];
ACTUAL -
The following line of code is not valid:
T = new T[length];

---------- BEGIN SOURCE ----------
public class myGenericClass<T> {
    private T[] genericArray;

    public myGenericClass (int length) {
        T = new T[length];    // genereic type arrays not allowed
    }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
I don't like this "workaround", but here it is:

public class myGenericClass<T> {
    private T[] genericArray;

    public myGenericClass (int length) {
        T = (T[])new Object[length];    // casting an Object[] to a T[]
    }
}

Comments
EVALUATION The language disallows arrays of parameterized types because of type erasure in the JVM. If generics are reified, then it would be possible to throw the appropriate ArrayStoreException when a parameterized-type array is assigned to unsafely.
04-08-2011