Not really a Java programmer here, but read some good papers about generics.
Yes, you should add some wildcard or the exact type (Class<MySpecialClass>
) to add safety. The reason is that Class is a generic. So, Class<Bar>
and Class<Foo>
are the same after erasure of their generic type parameter. They all become Class
, the so-called raw type. That erasure happens when compiling. Some example to illustrate this where the compiler helps you with automatic casting (exception handling omitted for brevity):
class Mine { }
class Vara {
public static void main(String... args) {
{ // works. translated to Mine m = (Mine) c.newInstance();
Class<Mine> c = Mine.class;
Mine m = c.newInstance();
}
{ // doesn't work. need a cast: Mine m = (Mine) c.newInstance();
Class c = Mine.class; // also Class<?> or Class<? extends Object>
Object o = c.newInstance(); // but this works. pointing to a Mine
Mine m = (Mine) c.newInstance(); // needs a manual cast
}
}
}
Saying Class<?>
(and the equivalent Class<? extends Object>
), you tell the compiler you really wanted a Class whose T
is Object, and didn't accidentally used the raw type. But it won't add any convenience casts. All that generics do is to insert automatic casts for you, to cast from Object
to the destination type. Generics are the same whether used with type U or with type T at runtime for compatibility reasons with older java versions.