Question

I recently read a comment saying that usage of serialVersionUID to make different versions of the same class compatible for serialization/deserialization is no longer needed from Java 5 onwards. Is this correct? Maybe it got mixed-up with covariant return types. So wanted to check it out with you all.

Was it helpful?

Solution

The JavaDocs for Java 5 still states that it is needed:

If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID value 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 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 InvalidClassExceptions during deserialization. Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value. It is also strongly advised that explicit serialVersionUID declarations use the private modifier where possible, since such declarations apply only to the immediately declaring class--serialVersionUID fields are not useful as inherited members.

OTHER TIPS

Since Java 1.4 you have been able to use XMLEncoder for Serialisation and this ignores any serialVersionUID, as does most other serialisation libraries.

However if you use ObjectOutputStream, its usage of serialVersionUID has not changed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top