When Java class implements Serializable interface you have the following options:

  • don’t declare serialVersionUID explicitly
  • add default serialVersionUID(1L)
  • generate serialVersionUID value

IntelliJ IDEA - serialVersionUID

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’”

IntelliJ IDEA - serialVersionUID

  • Then use the IntelliJ IDEA light bulb or press Alt+Enter to generate the field

IntelliJ IDEA - serialVersionUID

Image Credits: Labs64