COMP 310

serialVersionUID Warnings and Auto-Generation

    Current Home  Java Resources  Eclipse Resources

A very common compiler warning to receive is the following:

The serializable class XXX does not declare a static final serialVersionUID field of type long

The short answer for this is that any class that implements the Serializable interface needs to have a special static field called "serialVersionUID" that helps the Java system reliably serialize and de-serialize (reconstruct) objects by providing a unique identifier for the class.   Serialization is used whenever objects are shipped from one place to another, such as across a network.   Since most Swing components are Serializable , this warning comes up frequently when working with GUI components.

Since the serialVersionUID should be unique for every Serializable class in your system, it can be very difficult to insure that one has not duplicated a value somewhere.   Thus, it is not recommended that one attempt to hand-number all the serialVersionUID's in the system.    The serialVersionUID field should be private if possible.

Luckily,  Eclipse has a way to automatically generate serialVersionUID's.   When the above warning is encountered, either by accident or on purpose, by deliberately leaving out the serialVersionUID declaration,

  1. Right-click the warning and select "Quick Fix" or simply highlight it and type Ctrl-1  (one).
  2. Select the option "Add generated serial version ID".     (Don't select the initially highlighted choice, "Add default serial version ID" because this will create the same value for every class.)
  3. If the warning occurs in multiple places, the dialog box will show them in its lower "Problems" panel.  Be sure all the locations you want the serialVersionUID added are checked off.
  4. Click "Finish" and Eclipse will automatically create the serialVersionUID field and its value.

Quick Fix dialog for serialVersionUID warning


© 2017 by Stephen Wong