Serialization

Sure! Let’s go step by step and cover serialization and deserialization in Java in a clear way.


1. What is Serialization?

Serialization is the process of converting a Java object into a byte stream, so it can be:

  • Saved to a file
  • Sent over a network
  • Stored in a database

This allows the object’s state to be preserved and restored later.

Key Points:

  • Only the state of the object (its fields) is saved, not the behavior (methods).
  • The class must implement the Serializable interface.

2. What is Deserialization?

Deserialization is the reverse process of serialization:

  • Converting a byte stream back into a Java object.
  • The object can then be used as a normal Java object again.

3. How to Serialize an Object

  1. Make the class implement java.io.Serializable.
  2. Use ObjectOutputStream to write the object to a file.

Example:

import java.io.*;

class Student implements Serializable {
    private String name;
    private int age;

    Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String toString() {
        return "Student{name='" + name + "', age=" + age + "}";
    }
}

public class SerializeDemo {
    public static void main(String[] args) {
        Student s1 = new Student("Alice", 20);

        try {
            FileOutputStream fileOut = new FileOutputStream("student.ser");
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(s1); // Serialize the object
            out.close();
            fileOut.close();
            System.out.println("Serialization done.");
        } catch (IOException i) {
            i.printStackTrace();
        }
    }
}

4. How to Deserialize an Object

Use ObjectInputStream to read the object back.

import java.io.*;

public class DeserializeDemo {
    public static void main(String[] args) {
        Student s = null;

        try {
            FileInputStream fileIn = new FileInputStream("student.ser");
            ObjectInputStream in = new ObjectInputStream(fileIn);
            s = (Student) in.readObject(); // Deserialize
            in.close();
            fileIn.close();
        } catch (IOException i) {
            i.printStackTrace();
            return;
        } catch (ClassNotFoundException c) {
            System.out.println("Student class not found");
            c.printStackTrace();
            return;
        }

        System.out.println("Deserialized Student: " + s);
    }
}

5. Important Notes

  1. Serializable Interface:
    It’s a marker interface (no methods) used to indicate the class can be serialized.
  2. transient Keyword:
    Fields marked as transient are not serialized. private transient int ssn;
  3. serialVersionUID:
    It’s a unique ID to ensure the class is compatible during deserialization. private static final long serialVersionUID = 1L;
  4. Exceptions:
    • NotSerializableException → if class doesn’t implement Serializable.
    • IOException and ClassNotFoundException are common during serialization/deserialization.

Summary Table

ConceptPurpose
SerializationConvert object → byte stream
DeserializationConvert byte stream → object
SerializableMarker interface for serializable classes
transientSkip fields during serialization
serialVersionUIDVersion control for serialized objects

Leave a Reply