Typically, I\'ve seen people use the class literal like this:
Class cls = Foo.class;
But what if the type is generic, e.g. List?
The Java Generics FAQ and therefore also cletus' answer sound like there is no point in having Class, however the real problem is that this is extremely dangerous:>
@SuppressWarnings("unchecked")
Class> stringListClass = (Class>) (Class>) List.class;
List intList = new ArrayList<>();
intList.add(1);
List stringList = stringListClass.cast(intList);
// Surprise!
String firstElement = stringList.get(0);
The cast() makes it look as if it is safe, but in reality it is not safe at all.
Though I don't get where there can't be List>.class = Class since this would be pretty helpful when you have a method which determines the type based on the generic type of a >
Class argument.
For getClass() there is JDK-6184881 requesting to switch to using wildcards, however it does not look like this change will be performed (very soon) since it is not compatible with previous code (see this comment).