This Java program I got from a blog does not compile:
import java.util.*;
class Egg
{
}
class Muffin
{
}
public class Breakfast
{
private List<Egg> eggs;
private List<Muffin> muffins;
public void add(List<Egg> moreEggs) {
this.eggs.addAll(moreEggs);
}
public void add(List<Muffin> moreMuffins) {
this.muffins.addAll(moreMuffins);
}
}
The error message is:
name clash: add(java.util.List<Muffin>) and add(java.util.List<Egg>) have the same erasure
I was amazed when I saw that. I finally found an explanation in this forum.
What is happening is that (I quote) “Parameterized types (aka Generics) are only to restrict the use to enter the right object into the collection. All the generic information is erased by the compiler.”
So that why I got a name clash error. Both add(java.util.List<Muffin>) and add(java.util.List<Egg>) are in fact the same method add(java.util.List)!!!
Generics in Java are only used at compile time. They don’t even exist at run time!
PS:
I am a fan of Erasure, the group. Vince Clark was the initiator of Depeche Mode after all…