Wednesday 18 April 2012

Serialization in Java

What is Serialization?


It is the process of converting the object state to the sequence of bytes. The serialization is used for transferring an object from on JVM to another or for storing the data into a file system etc...
It is widely used for replicating an object in one JVM machine to another JVM machine.

When Serialization is useful?


Transferring an object from JVM to JVM : When you are making the remote calls from client to server either through RMI or EJB, the parameter objects passed in method call must be serialized. These objects, which are on the client, are replicated on the server.


Storing an object in the file system:
When you would like to persist the object in flat file system, the object which needs to be persisted in the file system has to be serialized. So that, the object can converted into the stream of bytes to store it in the file system.

How to Serialize an Object/Class?

Class is a blue print of the object. If you want to let an object to serialize, The java.io.Serializable class must be implemented by the class. This is a marker interface. It does not have any methods to implement. If any of the super class or interface is implementing the Serializable interface then your class need not implement it again as because those properties might have already acquired by your class with inheritance.

Must know in Serialization: 


  1. All instance variables in the class must be Serializable. If any instance variable not implementing Serializable interface wold through an exception during the Serialization process.
  2.  The static variables are not serialized. Serialization is the process of replicating the instance. Here the static variables are not copied because, the static variables are per class. It is of no use and not valid to copy a static variable from one JVM to another. Think over it.
  3. The instance variable designated with "transient" keyword are not serialized. The transient keyword is used, when a particular instance variable don't want to be serialized along with holding instance because that instance variable is not implementing the Serializable interface etc..
  4. Most of the pre defined java classes are serialized such as Collections, String and arrays etc...
  5. Serialization does not care about the access modifiers. All the instance variables,except designated with transient, will be serialized.

How an object is serialized (internal steps of Serialization process):

  1. Meta data of the JVM,such as JVM version etc..., will be wrote initially in byte stream. This data is called as magic data.
  2. Meta data of the class,such as length of the class,name of the class and serialVersionUID(More about serialVersionUID can be found at What is serialVersionUID? ) etc..,, which is associated with this instance will be wrote into the byte stream.
  3. It recursively writes out the meta data of its super class till the object class.
  4. Once the meta data information is wrote, it start writing the actual content of the instance start from the instance variables super class to the associated class of the instance variable.


No comments:

Post a Comment