Why SerialVersionUID is Static

I read about Serialization in java.Java says that static variables are not serialized with object.serialVersionUID is required during serialization. When an object is serialized, serialVersionUID is serialized along with other content. This is one exception to the general serialization rule that "static fields are not serialized"

Can someone tell me why this static.it can be non-static?

+4
source share
6 answers

Because any of the object variables / members can be obtained as soon as the object is successfully created. You cannot access object variables without creating it. Returning to the question, during deserialization, the object must be created from the data. If there is no way to check whether an object is deserializable, there is no way to get Object elements.

For the same reason, the UID is created as static.

+5
source

You must first understand what serialVersionUID does.

From java.io.Serializable :

Serialization of serialization is associated with each serializable class, a version number called serialVersionUID, which is used during deserialization to verify that the sender and receiver of the serialized object have loaded classes for this object that are compatible with regards to serialization. If the receiver has loaded the class for an object with a different serialVersionUID than the corresponding sender class, then deserialization will result in an InvalidClassException.

serialVersionUID defines compatibility between different versions of a class. Since the property is associated with the class, it must be made static .

+1
source

This serialVersionUID is an agreement to verify that you have the same binary versions in both the serializer and the deserializer, and nothing will go wrong with older versions. In addition, this may be the case when you have the same name for different classes of classes in the deserializer, so serialVersionUID is like a unique identifier of the form od.

This is why it must be static (therefore, it is "limited by the class, not the whole instance) and is" sent "along with serialized data.

0
source

It must be static, as the JRE must have it. The reasoning can be simple, it can be accessed in a static way, without actually creating an object of the class.

From javadocs :

Serialization of serialization is associated with each serializable class, a version number called serialVersionUID, which is used during deserialization to verify that the sender and receiver of the serialized object have loaded classes for this object that are compatible with regards to serialization. If the receiver has loaded the class for an object with a different serialVersionUID than the corresponding sender class, then deserialization will result in an InvalidClassException. A serializable class can declare itself serialVersionUID explicitly by declaring a field named " serialVersionUID", which must be static, final and of type long:

0
source

It is an exception that it is serialized. I believe this is static because it is associated with the class, not with any instance of the class. All class instances must adhere to the same serialVersionUID . Again, the old statics means that the runtime can actually get this identifier without creating any object, while the precondition for de-serializing the object is to set the identifier. When an object is serialized, the class name, state of the object (non-static member variables) and serialVersionUID are saved.

0
source

serialVersionUID is static because it does not apply to an instance of the class, but to the class itself. It is stored in the handle to the ObjectOutputStream class.

0
source

Source: https://habr.com/ru/post/1493352/


All Articles