lily
lily

Reputation: 565

what parameterized type should be for a "List" of different classes in Java?

what parameterized type should be for the "List" below?

  List<Class<?>> supportedClasses = Arrays.asList(String.class, Integer.class, Long.class);
        

according to IDEA enter image description here

I tried

enter image description here

also

enter image description here

but they all have grammer error

Upvotes: 0

Views: 95

Answers (2)

bcsb1001
bcsb1001

Reputation: 2907

This error occurs in Java 7 because the compiler cannot infer the type of the list returned from Arrays.asList. JEP 101, released in Java 8, adds this functionality. The workaround for Java 7 is to explicitly specify the method type parameter:

List<Class<?>> supportedClasses = Arrays.<Class<?>>asList(String.class, Integer.class, Long.class);

Upvotes: 0

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79105

Since the elements of your list are of type, Class, your list should be of type, List<Class<?>>

List<Class<?>> supportedClasses = Arrays.asList(String.class, Integer.class, Long.class);

Upvotes: 5

Related Questions