Should I use question mark type argument or suppress rawtypes warning?

Since the introduction of generic type parameters (or type arguments) in Java, writing the following line in Eclipse IDE will show a yellow squiggly line on the type Class:

Class myClass;

The warning shown on a mouseover is the following, with the two options (among others):

Class is a raw type. References to the generic type Class<T> should be parameterized.

  • Add type arguments to ‘Class
  • Add @SuppressWarningsrawtypes‘ to ‘myClass

The first option produces this code:

Class<?> myClass;

The second one produces this:

@SuppressWarnings("rawtypes")
Class myClass;

Both of the above options are equally adequate in taking care of the warning.

Assuming neither we nor Eclipse can infer generic type arguments*; what is the better approach to take, in what situations, and why?

* (despite having the option to try to do so)


Source: eclipse

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.