public class POJO<T> {
private List<Integer> integer = new ArrayList<Integer>();
public POJO() {
integer.add(1);
integer.add(2);
}
public List<Integer> getInteger() {
return integer;
}
public static void main(String[] args) {
POJO pojo = new POJO();
List<String> integer = pojo.getInteger(); // No compile error?
System.out.println(integer); // prints [1, 2]
}
}
How is it possible for the following line to compile:
List<String> integer = pojo.getInteger();
Provided getInteger() is typed as following
public List<Integer> getInteger()
I found a reference in the JLS 4.8 that backs up what @PeterLawrey says:
So all instance methods of your raw
POJOobject are erased, including those that don’t reference the typeTofPOJO<T>, which means (JLS 4.6):