public static List<Long> abc = new ArrayList<Long>(){{ //Asks for SerialVersionUID
abc.add(5L);
abc.add(7L);
}};
public static List<Long> abc = new ArrayList<Long>();//Does not need SerialVersionUID
static{
abc.add(5L);
abc.add(7L);
}
public static List<Long> abc = new ArrayList<Long>(){{ //Asks for SerialVersionUID abc.add(5L); abc.add(7L); }}; public
Share
In the second example, you’re instantiating a class that already has a defined
serialVersionUID(i.e.ArrayList).In the first example, you’re defining an anonymous subclass of
ArrayList, and your subclass needs to have its ownserialVersionUIDdefined. It’s not always obvious that double-brace initialization actually defines an anonymous class.