Friday, 11 December 2015

Why SerialVersionUID is static?


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 is 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.

The serialVersionUID determines compatibility between different versions of a class. Since the property is bound to the class, it has to be made static.


static variables are not serialized with the object.

However, serialVersionUID is a must in serialization process.


When an object is serialized, the serialVersionUID is serialized along with the other contents. This is one exception to the general serialization rule that, “static fields are not serialized”.

References:
http://docs.oracle.com/javase/6/docs/platform/serialization/spec/class.html#4100

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...