This line does not compile:
test(E1.class,E2.class);
There is only one parameter of type E , and Java must exactly match the expected argument types. It cannot output Example , because the objects are Class<E1> and Class<E2> , not Class<Example> . The invariance of Java generics prevents this.
You can get around this by introducing a wildcard on the upper bound in the test parameter of a general type:
public static <E extends Example> void test(Class<? extends E>... es)
This allows Java to infer Example for E by doing a wildcard on the upper bound with E1 and E2 .
The second line creates an unhandled Class es array, bypassing generics and generating an "unchecked call" warning.
new Class[]{E1.class,E2.class}
If you try to provide an argument of type Class here, you will get a compiler error with any parameter of a reasonable type halfway:
// Needs Class<Example> but found Class<E1> and Class<E2> test(new Class<Example>[]{E1.class,E2.class}); // Needs Class<E1> but found Class<E2> test(new Class<E1>[]{E1.class,E2.class}); // Needs Class<E2> but found Class<E1> test(new Class<E2>[]{E1.class,E2.class});
Satisfying the output with a wildcard here just reveals the real problem here is creating a shared array.
source share