Reputation: 783
How deserialization process is aware of serialVersionId of the serialized object when serialVersionId is not stored in the serialized object which is required to compare with serial version id of the class it will be converted to, to check if it is compatible?
Upvotes: 1
Views: 81
Reputation: 310907
When the object is serialized, first a descriptor of its class is serialized (once only), and that descriptor contains the serialVersionUID
. It isn't transmitted as part of the object's static state.
See newClassDesc
in the Object Serialization Specification, Object Serialization Stream Protocol chapter.
Upvotes: 0
Reputation: 10298
This article describes the serialized form of a Java object:
AC ED: STREAM_MAGIC. Specifies that this is a serialization protocol.
00 05: STREAM_VERSION. The serialization version.
0x73: TC_OBJECT. Specifies that this is a new Object.
0x72: TC_CLASSDESC. Specifies that this is a new class.
00 0A: Length of the class name.
53 65 72 69 61 6c 54 65 73 74: SerialTest, the name of the class.
05 52 81 5A AC 66 02 F6: SerialVersionUID, the serial version identifier of this class.
(fields omitted)
Upvotes: 2
Reputation: 10568
serialVersionUID is a static final long
that is serialized with its class.
If you do not manually provide one for your class, then the serialization runtime will calculate a default serialVersionUID value.
You can find more details on: http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html
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. Array classes cannot declare an explicit serialVersionUID, so they always have the default computed value, but the requirement for matching serialVersionUID values is waived for array classes.
Upvotes: 1