Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why SerialVersionUID is static

I am reading on Serialization in java.Java said that static variables are not serialized with the object.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”

Can anybody tell me why it is static.it can be nonstatic ?

like image 538
MayurB Avatar asked Jul 25 '13 05:07

MayurB


2 Answers

Because any of the object variables/members can be accessed once Object is successfully created. You can't access Object variables without creating it. Now back to question, during deserialization, Object needs to be created from the data. If there's no way to check whether object is deserializable, there's no way to get the Object members.

For the same reason, UID is made as static.

like image 200
RaceBase Avatar answered Oct 06 '22 19:10

RaceBase


serialVersionUID is static because it applies not to class instance but class itself. It is saved in ObjectOutputStream class descriptor.

like image 20
Evgeniy Dorofeev Avatar answered Oct 06 '22 19:10

Evgeniy Dorofeev