I got this one from a google I/O puzzler talk given by Joshua Bloch. Here’s the code
public class Glommer<T> {
String glom(Collection<?> obj){
String result = "";
for(Object o : obj){
result += o;
}
return result;
}
int glom(List<Integer> ints){
int result = 0;
for(int i : ints){
result += i;
}
return result;
}
public static void main(String args[]){
List<String> strings = Arrays.asList("1", "2", "3");
System.out.println(new Glommer().glom(strings));
}
this main method throws an exception because new Glommer is a raw type and hence all the generics in Glommer is erased, so it ends up calling int glom(List<Integer> ints) rather than String glom(Collection<?> obj).
My question is, even if I called glom() as new Glommer<Integer>().glom(strings) shouldn’t it call the int glom(List<Integer> ints) method since due to type erasure, this method is effectively int glom(List ints) and strings is of type List not Collection?
The called method is defined at compilation time, not at runtime.
If you add a parameter to your constructor call, the compiler will have enough information to know that it has to call the first method. Otherwise, it’s just as if generics didn’t exist. In both case, the called method will always stay the same at runtime.
EDIT Some people seem to doubt, so here’s another example:
The result is:
You pass an Integer to your method, but all that the compiler knows at compile time is that it’s an object. The jvm doesn’t automagically change the method call even though the Object is actually an Integer.