Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good practice to replace Class with Class<? extends Object> to avoid warnings?

In a bunch o' places in my code, I have something like this:

public Class mySpecialMethod() {
  return MySpecialClass.class;
}

which causes the warning

Class is a raw type. References to generic type Class should be parameterized.

But, if I replace

Class

with

Class<? extends Object>

the warning goes away.

Is this simple practice ok, or could it cause trouble later?

like image 810
user46277 Avatar asked Dec 16 '08 06:12

user46277


2 Answers

It's the correct thing to do only if there really is no common base class or interface that the Class object needs to represent.

Also Class<?> is effectively the same as Class<? extends Object>.

like image 78
Joachim Sauer Avatar answered Sep 19 '22 12:09

Joachim Sauer


Yes, it's totally right.

It's a requirement that you specify the type. And if you can't, you have to specify the wildcard.

Further reading: Java Language Specification: Parameterized Types

like image 35
guerda Avatar answered Sep 20 '22 12:09

guerda