views:

8355

answers:

7

There's a good discussion of Generics and what they really do behind the scenes over at http://stackoverflow.com/questions/31693/differences-in-generics so we all know that Vector < int[]> is a vector of integer arrays, and HashTable< String, Person> is a table of whose keys are strings and values Persons.

What stumps me is the usage of Class<>. The java class Class is supposed to also take a template name, (or so I'm being told by the yellow underline in eclipse). I don't understand what I should put in there. The whole point of the Class object is when you don't fully have the information about an object, for reflection and such. Why does it make me specify which class the Class object will hold? I clearly don't know, or I wouldnt be using the Class object, I would use the specific one.

+3  A: 

From the Java Documentation:

[...] More surprisingly, class Class has been generified. Class literals now function as type tokens, providing both run-time and compile-time type information. This enables a style of static factories exemplified by the getAnnotation method in the new AnnotatedElement interface:

<T extends Annotation> T getAnnotation(Class<T> annotationType);

This is a generic method. It infers the value of its type parameter T from its argument, and returns an appropriate instance of T, as illustrated by the following snippet:

Author a = Othello.class.getAnnotation(Author.class);

Prior to generics, you would have had to cast the result to Author. Also you would have had no way to make the compiler check that the actual parameter represented a subclass of Annotation. [...]

Well, I never had to use this kind of stuff. Anyone?

+5  A: 

Using the generified version of class Class allows you, among other things, to write things like

Class<? extends Collection> someCollectionClass = someMethod();

and then you can be sure that the Class object you receive extends Collection, and an instant of this class will be (at least) a Collection.

Yuval
A: 

You often want to use wildcards with Class. For instance, Class<? extends JComponent>, would allow you to specify that the class is some subclass of JComponent. If you've retrieved the Class instance from Class.forName, then you can use Class.asSubclass to do the cast before attempting to, say, construct an instance.

Tom Hawtin - tackline
+1  A: 

As other answers point out, there are many and good reasons why this class was made generic. However there are plenty of times that you don't have any way of knowing the generic type to use with Class<T>. In these cases, you can simply ignore the yellow eclipse warnings or you can use Class<?> ... That's how I do it ;)

bruno conde
A: 

I have found class<T> useful when I create service registry lookups. E.g.

<T> T getService(Class<T> serviceClass)
{
    ...
}
Kire Haglin
+1  A: 

Sun has a tutorial on Generics that talks about raw types with examples: http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf [pdf]

Ryan Anderson
A: 

It is confusing in the beginning. But it helps in the situations below :

class SomeAction implements Action {
}

// Later in the code.
Class<Action> actionClass = Class.forName("SomeAction"); 
Action action = actionClass.newInstance();
// Notice you get an Action instance, there was no need to cast.
fastcodejava
Isn't that just an incredibly complicated way of saying Action a = new Action() ?
Karl
`Action a = new Action() `? `Action` i an interface, it is `SomeAction` we are trying get an instance of. We only have the name of `SomeAction` available at runtime.
fastcodejava