What is the difference between those two. Why does the latter create a new serializable class?
new ArrayList<Clazz>()
creates a new empty ArrayList
new ArrayList<Clazz>(){}
Eclipse shows: The serializable class does not declare a static final serialVersionUID field of type long
In the first example, you’re creating an
ArrayListinstance. In the latter you’re creating instance of an anonymous subclass ofArrayList. Usually you would override one or more methods in the subclass,otherwise there’s not much point in creating such.As John Skeet points out, there is one hacky reason to create an anonymous subclass of a generic type, see his answer.The Eclipse warns that, in order to adhere to the
Serializablespecifications (ArrayListisSerializable, so all its subclasses are too), you should define an uniqueserialVersionUIDin the subclass from which the deserialization process can ensure that the class definition has not changed significantly since it was serialized (significantly == you yourself have decided that the new definition is incompatible with the old one, so you can express the fact by changing theserialVersionUID). If you’re never going to serialize the list, then the warning doesn’t matter.