Mar 22, 2012

Java Serialization

Java Serialization Tutorial



Java Serialization is an advanced topic with many options to make the serialization operation as flexible as required. This operations is used to persist the state of objects as streams and then used as stream or files. The advantage of using Java Serialization is the fact that we can later load the state of object. The practical use is to transmit object state over network as with Java exceptions and restoring application state after a system crash.

Some important points to note about Java Serialization are:

1) The class needs to implement java.io.Serializable interface which is a marker interface.
2) One can serialize objects by using ObjectOutputStream objects.
3) When we de-serialize the object, the constructor of this class is not invoked but constructor of super class starting from first non-serializable classes are invoked.
4) We can avoid the serialization of particular member variable by making them as static or transient.
5) We can customize the serialization operation by using serialVersionUID and also using Externalizable interface.
6) The default serialVersionUID changes if we change any out of class name, interfaces being implemented, classes being extended, member variables or member methods.
7) If serializable class has a non-serializable member reference variable then an exception will be raised while serializing the parent class's objects.
8) If you are implementing Singleton pattern then make sure to use readResolve method to avoid breaking the singleton pattern while de-serialization operation.
9) The following is a sample code for writing Serializable class and performing the serialization operation on the objects of this Serializable class:

SuperSerializable.java

package com.example;

public class SuperSerializable {
public SuperSerializable(){
}

public SuperSerializable(int a){
System.out.println("Inside super constructor");
}
}


SubSerializable.java

package com.example;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class SubSerializable extends SuperSerializable implements Serializable{

public SubSerializable() {
super(1);
}

public static void main(String[] args) throws Exception{
SubSerializable sub = new SubSerializable();

System.out.println("Serializing");
FileOutputStream fos = new FileOutputStream(new File("c://test"));
ObjectOutputStream oos = new ObjectOutputStream(fos);

oos.writeObject(sub);

System.out.println("De-Serializing");
FileInputStream fis = new FileInputStream(new File("c://test"));
ObjectInputStream ois = new ObjectInputStream(fis);
sub = (SubSerializable)ois.readObject();
}
}

2 comments:

  1. Running the above program i am getting
    com.example.SubSerializable; no valid constructor

    Can you explain ?
    Thanks

    ReplyDelete
  2. @Pank, We need to have a no arg public constructor in the super class of Serializable class. Note that this no-arg constructor is not required if the super class is Serializable itself

    In the above code, either we can mark the SuperSerializable class as serializable or add a public no-arg constructor. I have added pubic no-arg constructor.

    ReplyDelete