My interview asked what is serialization. I said " Serilazation is the process of storing the state of object by converting it in to bytestreams. when the object transferred into network that time you need serialization(used in RMI) .1) Implementing serializable interface and another way of 2) writeObject , readObject to do serialization. I need clear explanation please. i googled but not clear
Joined: Mar 22, 2005
Serialization is a more general process than you describe; it needn't result in binary data. For example, if the objects in question adhere to JavaBeans conventions, then you can use the java.beans.XMLEncoder/XMLDecoder classes to obtain a serialized XML representation of the objects.
When you stop or restart an application in the Tomcat application server, Tomcat will serialize all of the session objects and save them to disk.
When the app starts up again it reads them from disk and loads them back into memory. This allows you to restart the server or an application without interrupting people's sessions.
As Ulf mentioned: This won't occur unless all the objects bound to session implement Serializable and are serializable. Something like a socket or a database connection, for example can not be serialized to disk.
Serialization is a process of persisting an object's state. For serialization, it is necessary that the object's class or any class above it in the inheritance tree (superclass) must implement the marker interface "Serializable" (i.e. if a class is serializable, then all the class below in the inheritance tree will be automatically serializable). Also if the class has instance reference variable to any other class's object then that referred class must be also serializable otherwise it will give a Runtime Exception during serialization.
If you don't want to serialize any instance variable then declare it transient. Hence, it will be skipped from being serialized.
During deserialization, the object's state will be restored and all the instance variables will get their state back except the transient instance variables which will get its default value.
Note that during deserialization,
1- the constructor of the object's class is not called.
2- The constructor of any class above it in the inheritance tree is not called if they are serializable. For e.g.
So if C's object is deserialized then A's constructor will be called but not B's. Hence, A's inherited part in C will get different value as per the A's constructor but B's part will get the same value as persisted.
Joined: Oct 13, 2005
Welcome to the Ranch , Sujeet Kumar Jaiswal
Sujeet Kumar Jaiswal
Joined: Mar 07, 2010
Rajendra Prakash wrote:what is mean by storing the state of an object.
An object has state (instance variables) and behavior (methods). When an object is constructed, its instance variables are assigned some value (i.e. the object's state is set) as per the constructor definition. And then during the lifetime of the object, its state may change (i.e. the instance variable's value may change).
Once we decide to serialize this object, it means that we want to persist the current state(instance variable's value) of the object.