Consider the following code:
public class Main {
public static class NormalClass {
public Class<Integer> method() {
return Integer.class;
}
}
public static class GenericClass<T> {
public Class<Integer> method() {
return Integer.class;
}
}
public static void main(String... args) {
NormalClass safeInstance = new NormalClass();
Class<Integer> safeValue = safeInstance.method();
GenericClass unsafeInstance = new GenericClass();
Class<Integer> unsafeValue = unsafeInstance.method();
}
}
If I compile it with:
$ javac -Xlint:unchecked Main.java
It returns:
Main.java:16: warning: [unchecked] unchecked conversion
Class<Integer> unsafeValue = unsafeInstance.method();
^
required: Class<Integer>
found: Class
1 warning
Please note that only the generic method is considered unsafe, even if no generic type is referenced on the return type.
Is this a javac
bug? Or there is a deeper reason for this I'm not taking into account?
public Class method()
instead ofpublic Class<Integer> method()
. – Isobaric