Why isn't Array.newInstance(Class<?>, int) generic?

Viewed 1351

I mean, is there any good reason for that? The method's got the following signature:

public static Object newInstance(Class<?> componentType,
                 int length)
                          throws NegativeArraySizeException

In my opinion, it would be much more convinient to declare the method as follows:

public static <T> T[] newInstance(Class<T> componentType, int length) 
                                          throws NegativeArraySizeException

That way, when creating an aray of generic type it would not be neccesary to perform additional casting, e.g.

public class Gen<T>{
    private T[] a;

    public static <T> Gen<T> createByClass(Class<T> clazz){
        a = clazz.cast(Array.newIntance(clazz.getComponentType(), 8); //we have to invoke 
                                                                      //clazz.cast here.
    }
}

Was there any good reason to declare the return value type as Object? To me it seems very incovinient.

1 Answers
Related