Jump to content




Recent Status Updates

  • Photo
      19 Sep
    Chall

    I love it when you go to write a help thread, then while writing, you reach an enlightenment, and figure it out yourself.

    Show comments (3)
  • Photo
      15 Sep
    Error

    Programming is something that I enjoy and want to make a career out of. But, I usually tend to start things and not finish them. Any advice on how I can finish what I start?

    Show comments (3)
View All Updates

Developed by Kemal Taskin
- - - - -

Tutorial: Java Serialization


  • Please log in to reply
No replies to this topic

#1 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 07 May 2008 - 01:34 PM

Java Serialization


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.

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.
FileOutputStream fOut = new FileOutputStream("SerializedFile.txt"); 
ObjectOutputStream outSt = new ObjectOutputStream(fOut); 
outSt.writeObject(b1);
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!

transient int i;
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
[I]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");
    }
[/I]}[I]
class Derived extends Base{
    Derived()
    {
        System.out.println("In Derived  Constructor");
    }
[/I]}[I]

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();
                 Base b4 = (Base)inSt.readObject();
                 b3.function();
                 b4.function();
                 inSt.close();
        } 
        catch(Exception e){
             e.printStackTrace();
        } 
    }
[/I]}
Output
In Base Constructor
In Base Constructor
In a function
In a function
End of serialization
In a function
In a function
Questions or Comments?
Ask your questions or comments here!
  • 0