Pages

Serialize and De-serialize java objects

In this post I will tell you about Java serialization and de-serialization.

Serialization is the process of storing the objects state (parameters) in a file, that can later be used.

De-serialization is the reverse process of serialization, means getting the saved state of an object.

Points to remember-

  1. Java provides an serializable (java.io.Serializable) interface. which is a marker interface that not contain any method declaration.  
  2. If you want to serialize an object of a class you must implements Serializable interface.
  3. use transient keyword to serialize only some part of the object. Suppose you does not want to some properties to be serialize during serialization process then mark these properties as transient.
  4. As static variable are the part of class they not take part in serialization

Here is an example how to serialize an object in Java -

package com.esc.test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class TestSerilizable implements Serializable {

    private static final long serialVersionUID = 1L;
    private int age;
    private String name;

    public TestSerilizable(int age, String name) {

        this.age = age;
        this.name = name;

    }

    @Override
    public String toString() {

        return age + " " + name;

    }

    public static void main(String[] args) throws IOException {

        TestSerilizable test = new TestSerilizable(11, "abhi");
        System.out.println(test.toString());

        TestSerilizable test2 = new TestSerilizable(12, "rani");
        System.out.println(test2.toString());

        FileOutputStream fileStream = new FileOutputStream("resources/test.ser");
        
        ObjectOutputStream objectStream = new ObjectOutputStream(fileStream);
        
        objectStream.writeObject(test);
        objectStream.close();
    }
}


de-serializing the serialized object
        FileInputStream inputStream = new FileInputStream("resources/test.ser");
        ObjectInputStream objInputStream = new ObjectInputStream(inputStream);
     
        TestSerilizable deSerializeValue =(TestSerilizable)objInputStream.readObject();
        System.out.println("Deserialize value  age  = " + deSerializeValue.age);
        System.out.println("Deserialize value  name  = " + deSerializeValue.name);

Now you get the value of your object after running the above program

Deserialize value  age  = 11
Deserialize value  name  = abhi

Mark some properties as transient that you do not want take part in serializtion

public class TestSerilizable implements Serializable {

    private static final long serialVersionUID = 1L;
    private int age;
    private transient String name; // not save in serialization

    public TestSerilizable(int age, String name) {

        this.age = age;
        this.name = name;

    }

    @Override
    public String toString() {

        return age + " " + name;

    }
}

Now run the de-serilization porogrma again

FileInputStream inputStream = new FileInputStream("resources/test.ser");
        ObjectInputStream objInputStream = new ObjectInputStream(inputStream);
     
        TestSerilizable deSerializeValue =(TestSerilizable)objInputStream.readObject();
        System.out.println("Deserialize value  age  = " + deSerializeValue.age);
        System.out.println("Deserialize value  name  = " + deSerializeValue.name);
        objInputStream.close();

output of the program. in this out put you can see value of name property is null because we mark it as transient, so during serialization its value not persist in the serialization. null is the default value of name as it is a string,

Deserialize value  age  = 11
Deserialize value  name  = null