How Generics at Class Level Parameter works
generics, java
Solution
With no generic type passed into constructor, all types are erased and the compiler is presented with this choices
String glom ( Collection );
int glom ( List );
The type is also erased from `strings` variable defined in `main`, so its type is `List`.
Because `List` is more specific than `Collection` it chooses `int glom ( List )`.
Now, if you have specified the generic parameter, then no type erasure happens, and compiler knows that it cannot match `int glom ( List<Integer> )` to `List<String>`, so it falls back to `String glom ( Collection<?> )`
Problem
Consider Following code from Java Puzzlers ``` class Gloam<T>{ String glom(Collection<?> objs ) { System.out.println("collection"); String result = ""; for (Object o : objs ){ result += o; } return result; } int glom(List <Integer> ints ) { System.out.println("List"); 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 Gloam().glom(strings)); } } ``` When I run this program it gives class cast exception, But if I provide any Generic argument for Gloam class in main method it works fine. ``` public static void main(String[] args) { List<String> strings = Arrays.asList("1", "2", "3"); System.out.println(new Gloam<Date>().glom(strings)); } ``` I don't understand how generic works in class type parameter ?