The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as
well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. Donald E. Knuth

Serialization in Java

Java supports a mechanism called Object Serialization in which an object is represented as an ordered sequence of bytes so that its state can be saved in a file or database or transmitted over the network. The object's state ( sequence of bytes ) can be read at a later point of time and this process is known as deserialization. The most crucial point about serialization is that an object can be serialized in one platform and deserialized in a different platform. Any class whose objects need to be serialized must implement the Serializable interface. The Serializable interface has no methods or fields. It is just a marker interface. The classes ObjectOutputStream and ObjectInputStream contains necessary methods which provides support for serializing and deserializing an object. Let's see how serialization works in Java.

Serializing and Deserializing an object
In the following program, we serialize an object of Student class and save it to a file. Then, we will deserialize the same object.

import java.io.*;
class Student implements Serializable {
   private int roll;
   private String name;
   Student(int r, String n) {
      roll = r; name = n;
   }
   public void displayData() {
      System.out.println("Displaying Student data :-");
      System.out.println(roll + "  " + name);
   }
}

public class SerializationDemo {
   public static void main(String args[]) {
      /* Object Serialization */
      try {
         Student s1 = new Student(7, "Rahul");
         FileOutputStream ofile = new FileOutputStream("stud.txt");
         ObjectOutputStream oos = new ObjectOutputStream(ofile);
         oos.writeObject(s1); // write the object to the file
         oos.close();
         ofile.close();
         System.out.println("Serialized data saved in file");
      } catch(Exception e) { }

      /* Object Deserialization */
      try {
         FileInputStream ifile = new FileInputStream("stud.txt");
         ObjectInputStream ois = new ObjectInputStream(ifile);
         System.out.println("Reading Serialized data from file");
         Student s1 = (Student)ois.readObject(); // read object from file
         s1.displayData();
         ois.close();
         ifile.close();
      } catch(Exception e) { }
   }
}

Note :-
1 ) The static data members of a class are not serialized since they belong to class as a whole and not to a      particular object.
2 ) If a class implements Serializable, then all its subclasses are also serializable.

The ' transient ' keyword
If we declare any instance variable as transient, then that variable won't be serialized. Thus, transient keyword is used to prevent serialization of any data member. If we attempt to deserialize the object, then a default value is returned for the transient variable. We have just modified the above program and made variable name of Student class as transient. If we try to deserialize any object of Student class, it will return null for name.

import java.io.*;
class Student implements Serializable {
   private int roll;
   private transient String name;
   Student(int r, String n) {
      roll = r; name = n;
   }
   public void displayData() {
      System.out.println("Displaying Student data :-");
      System.out.println(roll + "  " + name);
   }
}

public class TransientDemo {
   public static void main(String args[]) {
      /* Object Serialization */
      try {
         Student s1 = new Student(7, "Rahul");
         FileOutputStream ofile = new FileOutputStream("studnew.txt");
         ObjectOutputStream oos = new ObjectOutputStream(ofile);
         oos.writeObject(s1); // write the object to the file
         oos.close();
         ofile.close();
         System.out.println("Serialized data saved in file");
      } catch(Exception e) { }

      /* Object Deserialization */
      try {
         FileInputStream ifile = new FileInputStream("studnew.txt");
         ObjectInputStream ois = new ObjectInputStream(ifile);
         System.out.println("Reading Serialized data from file");
         Student s1 = (Student)ois.readObject(); // read object from file
         s1.displayData();
         ois.close();
         ifile.close();
      } catch(Exception e) { }
   }
}

Back | Next