Sunday, March 13, 2011

Java Serialization in gist

What is Serialization?
Simply put serialization is a method to deflate an object to store it on the system.

So how do you do it?
Simply implement the Serializable interface. Once you implement that interface, not only does your base class deflate, even you child classes deflate! What do I mean by all this, well read on.

Implementing Serialization

  1. Implement the interface serialzable.
    Code:
    class Example implements Serializable.


    The above step tells the complier that you are going to implement serialization.
    Which basically means that the object is going to be saved.



  2. Time to write the object to the disk.

    FileInputStream fIn = new FileInputStream("SerializedFile.txt"); 
    ObjectInputStream inSt = new ObjectInputStream(fIn);
    b3 = (ObjectType)inSt.readObject();

    This simply creates an output stream, pipes it up with an object output stream and writes the deflated object to disk. The name of the file is SerializedFile.txt.



  3. Then you decide to bring the objects back to life. So you follow the reverse process, which, naturally is called deserialization!

    FileInputStream fIn = new FileInputStream("SerializedFile.txt"); 
    ObjectInputStream inSt = new ObjectInputStream(fIn);
    b3 = (ObjectType)inSt.readObject();



  4. What if you have something top secret, like your password which should not be saved. You mark it as transient!




My guess is that you understood nothing till now. And rightfully so, its too much to take in one go. So here is a detailed theoretical explanation.


  • Serializaation is used to save an object and all the objects that inherit from the one serialized. They too are saved.
  • The objects whose references are in the serialized object is also saved!
  • The reason all these objects have to be saved is because when the main object is brought back to life, the references it points to must not be null. They should point to some valid objects.
  • That can only happen if all the objects referenced are saved with the base object.
  • In case you want that something should not be saved, you simply prefix it with the word transient.
  • When a serialized object is brought back to life, the transient variables are initialized to null.
  • Static variables, functions and classes cannot be serialized.
  • One very peculiar thing to note is that the constructors are not called again when the object is deserialized. This will be made amply clear by the example given below.


The Code


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Base implements Serializable{
Base()
{
System.out.println("In Base Constructor");
}
public void function()
{
System.out.println("In a function");
}
}
class Derived extends Base{
Derived()
{
System.out.println("In Derived Constructor");
}
}

public class SerializeObj {
public static void main(String[] args) {
Base b1 = new Base();
Base b2 = new Base();
//---------------Writing to disk------------------------
try{
b1.function();
b2.function();
FileOutputStream fOut = new FileOutputStream("SerializedFile.txt");
ObjectOutputStream outSt = new ObjectOutputStream(fOut);
outSt.writeObject(b1);
outSt.writeObject(b2);
outSt.close();
}
catch(Exception e){
e.printStackTrace();
}
System.out.println("End of serialization");
System.out.println("Begin Deserialization");
//---------------Reading from disk----------------------
try{
FileInputStream fIn = new FileInputStream("SerializedFile.txt");
ObjectInputStream inSt = new ObjectInputStream(fIn);
Base b3 = (Base)inSt.readObject();//note object created
Base b4 = (Base)inSt.readObject();
b3.function();
b4.function();
inSt.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}



Output

In Base Constructor
In Base Constructor
In a function
In a function
End of serialization
In a function
In a function

No comments:

Post a Comment

Chitika