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?
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>
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With