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 ?
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.
serialVersionUID is static because it applies not to class instance but class itself. It is saved in ObjectOutputStream class descriptor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With