The reason is that Test.class is of the type Class<Test>. You cannot assign a reference of type Class<Test> to a variable of type Class<T> as they are not the same thing. This, however, works:
Class<? extends Test> testType = type == null ? Test.class : type;
The wildcard allows both Class<T> and Class<Test> references to be assigned to testType.
There is a ton of information about Java generics behavior at Angelika Langer Java Generics FAQ. I'll provide an example based on some of the information there that uses the Number
class heirarchy Java's core API.
Consider the following method:
public <T extends Number> void testNumber(final Class<T> type)
This is to allow for the following statements to be successfully compile:
testNumber(Integer.class);
testNumber(Number.class);
But the following won't compile:
testNumber(String.class);
Now consider these statements:
Class<Number> numberClass = Number.class;
Class<Integer> integerClass = numberClass;
The second line fails to compile and produces this error Type mismatch: cannot convert from Class<Number> to Class<Integer>
. But Integer
extends Number
, so why does it fail? Look at these next two statements to see why:
Number anumber = new Long(0);
Integer another = anumber;
It is pretty easy to see why the 2nd line doesn't compile here. You can't assign an instance of Number
to a variable of type Integer
because there is no way to guarantee that the Number
instance is of a compatible type. In this example the Number
is actually a Long
, which certainly can't be assigned to an Integer
. In fact, the error is also a type mismatch: Type mismatch: cannot convert from Number to Integer
.
The rule is that an instance cannot be assigned to a variable that is a subclass of the type of the instance as there is no guarantee that is is compatible.
Generics behave in a similar manner. In the generic method signature, T
is just a placeholder to indicate what the method allows to the compiler. When the compiler encounters testNumber(Integer.class)
it essentially replaces T
with Integer
.
Wildcards add additional flexibility, as the following will compile:
Class<? extends Number> wildcard = numberClass;
Since Class<? extends Number>
indicates any type that is a Number
or a subclass of Number
this is perfectly legal and potentially useful in many circumstances.