A few obvious answers:
- At any time when the method does not actually accept the object as a parameter (for example, when you request an instance of a type from the DI or factory framework, for example, in the quartz example you presented).
- Each time the parameter you pass can be NULL, so you cannot call
getType on it, and instanceof will not indicate that the object is instanceof any type.
Thus, it makes sense to take the class argument when the method in question must either create an instance of the class in question or provide some metadata about this class that does not require instantiation.
The only other consideration that I would like to point out is that when accepting a class parameter, you can use generics, as in the following method signature on the Guice Injector interface:
<T> T getInstance(Class<T> type);
Note how this allows the method to declare that it returns an object of the same type that you pass as a parameter to the class, so you do not need to drop the object later. You can also use common wildcards ( Class<? extends Foo> ) to limit the types of classes that can be passed to a method.
source share