Please read the comments in the code in order, there are details of the question.
Why is this difference happening?
Please specify JLS if possible.
import java.util.*;
class Generic<T> {
T paramMethod() { return null; }
List<Integer> unrelatedMethod() { return null; }
}
@SuppressWarnings("unused")
public class Test {
void properUsage() {
Generic<String> g = new Generic<String>();
String s = g.paramMethod();
List<Integer> pos = g.unrelatedMethod();
List<String> thisShouldErrorCompile = g.unrelatedMethod();
}
void rawUsage() {
Generic g = new Generic();
String s = g.paramMethod();
List<Integer> pos = g.unrelatedMethod();
List<String> thisShouldErrorCompile = g.unrelatedMethod();
}
}
Side note
I initially found this in IntelliJ IDEA, but I think the compiler is compatible with javac, because when I compiled the above code with the following, it gave the same errors / warnings.
$ javac -version
javac 1.7.0_05
$ javac Test.java -Xlint:unchecked
...
$ javac Test.java -Xlint:unchecked -source 1.5 -target 1.5
...
source
share