COMP 310 |
serialVersionUID Warnings and Auto-Generation |
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,
© 2017 by Stephen Wong