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);
}

- 3,043
- 6
- 27
- 36
-
2This will answer your question and it is same http://stackoverflow.com/questions/924285/efficiency-of-java-double-brace-initialization – valli Dec 18 '09 at 06:26
-
1ack! in both examples you're creating a public static *modifiable* list -- very bad. – Kevin Bourrillion Dec 18 '09 at 08:00
-
1@Kevin What do you mean by in both examples you're creating a public static modifiable list. What's alternative? – DKSRathore Dec 18 '09 at 11:59
2 Answers
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 own serialVersionUID
defined. It's not always obvious that double-brace initialization actually defines an anonymous class.

- 398,947
- 96
- 818
- 769
Because in your first example you're creating an anonymous subclass of ArrayList via "double-brace initialization", and ArrayList implements the Serializable interface. The SerialVersionUID is used in deserialization, and it's a good practice to provide one, though not strictly necessary. Your IDE is probably configured to report these warnings.
In your second example, you are not creating an anonymous subclass of ArrayList, just instantiating one and calling its methods.

- 30,582
- 12
- 56
- 83