Eclipse issues warnings when a
serialVersionUID is missing.
The serializable class Foo does not declare a static final
serialVersionUID field of type long
serialVersionUID and why is it important? Please show an example where missing
serialVersionUID will cause a problem.
The docs for
java.io.Serializable are probably about as good an explanation as you’ll get:
The serialization runtime associates with each serializable class a version number, called a
serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different
serialVersionUIDthan that of the corresponding sender’s class, then deserialization will result in an
InvalidClassException. A serializable class can declare its own
serialVersionUIDexplicitly by declaring a field named
serialVersionUIDthat must be static, final, and of type
ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;
If a serializable class does not explicitly declare a
serialVersionUID, then the serialization runtime will calculate a default
serialVersionUIDvalue for that class based on various aspects of the class, as described in the Java(TM) Object Serialization Specification. However, it is strongly recommended that all serializable classes explicitly declare
serialVersionUIDvalues, since the default
serialVersionUIDcomputation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected
InvalidClassExceptionsduring deserialization. Therefore, to guarantee a consistent
serialVersionUIDvalue across different java compiler implementations, a serializable class must declare an explicit
serialVersionUIDvalue. It is also strongly advised that explicit
serialVersionUIDdeclarations use the private modifier where possible, since such declarations apply only to the immediately declaring class —
serialVersionUIDfields are not useful as inherited members.
If you’re serializing just because you have to serialize for the implementation’s sake (who cares if you serialize for an
HTTPSession, for instance…if it’s stored or not, you probably don’t care about
de-serializing a form object), then you can ignore this.
If you’re actually using serialization, it only matters if you plan on storing and retrieving objects using serialization directly. The
serialVersionUID represents your class version, and you should increment it if the current version of your class is not backwards compatible with its previous version.
Most of the time, you will probably not use serialization directly. If this is the case, generate a default
SerialVersionUID by clicking the quick fix option and don’t worry about it.
I can’t pass up this opportunity to plug Josh Bloch’s book Effective Java (2nd Edition). Chapter 10 is an indispensible resource on Java serialization.
Per Josh, the automatically-generated UID is generated based on a class name, implemented interfaces, and all public and protected members. Changing any of these in any way will change the
serialVersionUID. So you don’t need to mess with them only if you are certain that no more than one version of the class will ever be serialized (either across processes or retrieved from storage at a later time).
If you ignore them for now, and find later that you need to change the class in some way but maintain compatibility w/ old version of the class, you can use the JDK tool serialver to generate the
serialVersionUID on the old class, and explicitly set that on the new class. (Depending on your changes you may need to also implement custom serialization by adding
readObject methods – see
Serializable javadoc or aforementioned chapter 10.)