I have the following code, but it won't compile:
Class<? extends something>[] classes = new Class<? extends something>[5]();
Why exactly won't this work? And is there a way around this? I also tried it with Class<?> and that didn't work either.
I have the following code, but it won't compile:
Class<? extends something>[] classes = new Class<? extends something>[5]();
Why exactly won't this work? And is there a way around this? I also tried it with Class<?> and that didn't work either.
The answer has to do with Array Creation Expression.
The rule clearly states:
The rules above imply that the element type in an array creation expression cannot be a parameterized type, other than an unbounded wildcard.
That's why your above code will never compile. In fact, the following compile-time error message shows (example):
/**
*
*/
/**
* @author The Elite Gentleman
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Class<? extends Exception>[] classes = new Class<? extends Exception>[5];
}
}
Test.java:17: generic array creation
Solution (that works, if you follow the above rule):
Class<? >[] classes = new Class<?>[5];
The above line compiles.
I hope this helps.
i think you should try removing () from the last
Class<? extends something>[] classes = new Class<? extends something>[5];