1

I Created an array like this:

    Class<? extends AbstractItem>[] itemList = {
            ItemNew.class,
            ItemLoad.class,
            ItemSave.class,
            ItemSetting.class,
            ItemEnd.class
    };

but java tells me:

Cannot create a generic array of Class<? extends AbstractItem>  

If I don't specify the Class generics java warns me with:

Class is a raw type. References to generic type Class<T> should be parameterized    

So who is right? and even more why doesn't it allow me to limit the Class types?

edit: the mentioned duplicate is not solving my problem, cause if I do not specify the type of the array, I can create it with compiler warning. So it is indeed possible but I am looking for the right way to achieve this)

3
  • 1
    General rule of thumb: Don't combine generics and arrays. You only get headaches. Commented Aug 6, 2017 at 9:15
  • i can create it. if i leave it generic. but i was concerned about the compiler warning. the solution for me was to specefie it, what endet me with a error. Commented Aug 6, 2017 at 9:20
  • List<Class<? extends AbstractItem>> classes = Arrays.asList(ItemLoad.class, ItemNew.class); Commented Aug 6, 2017 at 9:21

1 Answer 1

0

The java don't allow create generic array except all type arguments are unbounded wildcards.

Consider the following code:

    List<String>[] strings=new List<String>[1];    //1
    List<Integer> integers=Arrays.asList(1);
    Object[] objects=strings;
    objects[0]=integers;
    String s=strings[0].get(0);
    System.out.println(s);      //2

See the statement 1,if java allow create the generic type array,the above code will get a exception java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String on statement 2 whitout any warnings at run-time.This is not correctly ,so the java mustn't create generic type array.

If you change List<String>[] stringList=new List<String>[1]; //1 to List<String>[] stringList=new List[1];,the code will can compile correctly,but the compiler will get a warning like Unchecked assignment,of course,your can use @SuppressWarnings("unchecked") to suppress the warning.

In the JLS 10.6 Array Initializers section,there is a rule:

It is a compile-time error if the component type of the array being initialized is not reifiable

and the JLS 4.7 Reifiable Types define what is reifiable types,the first one is :

It refers to a non-generic class or interface type declaration.
Sign up to request clarification or add additional context in comments.

1 Comment

The situation you're describing also arises (at compile time) without any generics (e.g. try it with String[], Object[] and assign a Boolean, it compiles). It's caused by assigning a Whatever[] array to a variable declared Object[], without even getting a compiler warning. In the non-generics situation, at least you get an ArrayStoreException at runtime.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.