If you do forget to update the field, you might end up with two different versions of a class with different structure but with the same serialVersionUID. In order to facilitate such a check, you must maintain the serialVersionUID just like before and not forget to update it when making changes to your classes.
You are only concerned with making sure that the code bases which are communicating indeed have the same versions of relevant classes. In this case you are not interested in maintaining backwards compatibility. It is far more common to use the serialization mechanism to temporarily write data to for instance a cache or send it over the network to another program with the same version of the relevant parts of the codebase. This versioning technique is useful if you store serialized data which survives several versions of your code.īut storing serialized data for such a long time span is not very common.
This custom code can then check the serialVersionUID in order to know which version the data is in and decide how to de-serialize it. But if you want to you can define your own readObject()-function which can read back old data.
The built-in de-serialization mechanism ( in.defaultReadObject()) will refuse to de-serialize from old versions of the data. If you want to version your data, you normally start with a serialVersionUID of 0, and bump it with every structural change to your class which alters the serialized data (adding or removing non-transient fields). When de-serializing, the same version is checked to see how the serialized data matches the current code. Its value is stored with the data when serializing. SerialVersionUID facilitates versioning of serialized data. (Depending on your changes you may need to also implement custom serialization by adding writeObject and readObject methods - see Serializable javadoc or aforementioned chapter 10.) 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. 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). Changing any of these in any way will change the serialVersionUID. Per Josh, the automatically-generated UID is generated based on a class name, implemented interfaces, and all public and protected members.
Chapter 10 is an indispensible resource on Java serialization. I can't pass up this opportunity to plug Josh Bloch's book Effective Java (2nd Edition). 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. Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value. 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. 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. A serializable class can declare its own serialVersionUID explicitly by declaring a field named serialVersionUID that must be static, final, and of type long: ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an 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. The docs for java.io.Serializable are probably about as good an explanation as you'll get: