Serialization

What is Serialization?

Serialization is a process of storing the state of object into a permanent storage (say in a file system or a database) and then retrieving it back as and when required which is called as Deserialization.

What happens to the methods of the class?

As stated in the definition, serialization is for the state of the object for which only the instance variables count and contribute and the methods do NOT.

What happens to the static values during serialization?


Basically the serialization is applied only for objects and not for class level variables. If you want it you need to manually overwrite the readObject() and writeObject() methods and do the part on your own.

For the detailed answers in the above thread, here you go.

By Ulf Dittmer:
There is no single "correct" value for static fields. Serialization simply ignores them. So if an object is deserialized, the static fields of that class remain what they were before the deserialization. If you want fields that can change on a per-object basis, they can't be static.

By Henry Wong:
If you really want to serialize/deserialize static variables, then you can implement the readObject() and writeObject() methods for the class. (technically, it is not "override" since these are private methods).

However, keep in mind, that when you deserialize, you will be changing a static variable — which is a variable used by every object of that class type.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.