When Java class implements Serializable interface you have the following options:
- don’t declare serialVersionUID explicitly
- add default serialVersionUID(1L)
- generate serialVersionUID value
Java API Specification says:
It is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassException’s during deserialization.
therefore it is wise to choose the latter option.
If you’re using a very popular Java development IDE IntelliJ IDEA, you may find it not straightforward, how to generate the serialVersionUID there. Below is the recipe for the IntelliJ IDEA 12 (latest at the time of writing), hope you’ll find it useful:
- Go to “File” => “Settings… (Ctrl+Alt+S)” => “Inspections” => “Serialization issues”
- … and enable “Serializable classes without ‘serialVersionUID’”
- Then use the IntelliJ IDEA light bulb or press Alt+Enter to generate the field
Image Credits: Labs64