Runtime serialization assigns a version number to every serialization class.
It's serialVersionUID
.
It performs authentication, such as consistency checks, when deserializing a serialized class.
If the serialVersionUID
of the deserialized class and the class in the class loader are different,
The deserialization process raises a ʻInvalidClassException` exception.
Serializable classes can define their own serialVersionUID
.
However, the serialVersionUID
field must be static, long, and final.
Example:
private static final long serialVersionUID = 42L;
If the serializable class does not explicitly define the serialVersionUID
field, then from runtime serialization
The value of serialVersionUID
is automatically generated by calculating based on the structure of the class such as the fields and methods of the class.
Nevertheless, it is highly recommended that all serializable classes explicitly declare serialVersionUID
.
The reason is that the calculation method of serialVersionUID
depends on the structure of the class (it seems to take a long time to calculate) and the Java compiler.
Unexpected ʻInvalidClassExceptions` exceptions may occur when doing deserialization time.
That's why the class explicitly sets serialVersionUID
to guarantee the same serialVersionUID
in different Java compiler environments.
Should be declared.
We also strongly recommend that you use the private modifier in the most explicit serialVersionUID
declaration.
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 serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException. 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 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.
Citation material What is a serialVersionUID and why should I use it?
Those who want to understand more What I know about the esoteric Serializable specification, or my understanding
Recommended Posts