Unlocking the Power of Java’s Serializable Interface

Ahad Arif
3 min readMar 5, 2023

--

A Comprehensive Guide to Object Serialization and Deserialization

What is the Serializable interface in JAVA?

The Serializable interface in Java is a marker interface that indicates that an object can be written to a stream and read back from a stream. The Serializable interface is part of the Java platform's object serialization mechanism, which provides a way to write objects to streams (such as files or network sockets) and read objects back from those streams.

Objects that implement the Serializable interface can be saved to disk or transmitted over a network as a sequence of bytes, and later restored to their original form. This allows objects to be passed from one JVM to another, or to persist the state of an object between program executions.

When an object is serialized, the Java runtime writes the object’s state to a stream, including the values of its instance variables. When the object is deserialized, the runtime creates a new object with the same state as the original.

The Serializable interface does not define any methods, and it is only used to indicate that an object is serializable. The actual serialization and deserialization of objects are performed by the Java runtime.

In some cases, it may not be appropriate to serialize an object, either because the object contains information that should not be written to a stream (such as passwords), or because the object contains references to other objects that are not serializable. In these cases, the object can be made not serializable by implementing the java.io.Externalizable interface instead of the Serializable interface.

Most impressive is that the entire process is JVM-independent, meaning an object can be serialized on one platform and deserialized on an entirely different platform.

Classes ObjectInputStream and ObjectOutputStream are high-level streams that contain the methods for serializing and deserializing an object.

The ObjectOutputStream class contains many writing methods for writing various data types, but one method, in particular, stands out −

public final void writeObject(Object x) throws IOException
public final Object readObject() throws IOException, ClassNotFoundException

How to create a class which can be serialized?

  • The class must implement the java.io.Serializable interface.
  • All of the fields in the class must be serializable. If a field is not serializable, it must be marked transient.
public class Employee implements java.io.Serializable {
public String name;
public String address;
public transient int SSN;
public int number;

public void mailCheck() {
System.out.println("Mailing a check to " + name + " " + address);
}
}

Serialization Example

import java.io.*;
public class SerializeDemo {
   public static void main(String [] args) {
Employee e = new Employee();
e.name = "Abdul Ahad";
e.address = "Wari,Dhaka";
e.SSN = 12345678;
e.number = 1748;

try {
FileOutputStream fileOut =
new FileOutputStream("/tmp/employee.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(e);
out.close();
fileOut.close();
System.out.println("Serialized data is saved in /tmp/employee.ser");
} catch (IOException i) {
i.printStackTrace();
}
}
}

Deserialization Example

import java.io.*;
public class DeserializeDemo {
   public static void main(String [] args) {
Employee e = null;
try {
FileInputStream fileIn = new FileInputStream("/tmp/employee.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
e = (Employee) in.readObject();
in.close();
fileIn.close();
} catch (IOException i) {
i.printStackTrace();
return;
} catch (ClassNotFoundException c) {
System.out.println("Employee class not found");
c.printStackTrace();
return;
}

System.out.println("Deserialized Employee...");
System.out.println("Name: " + e.name);
System.out.println("Address: " + e.address);
System.out.println("SSN: " + e.SSN);
System.out.println("Number: " + e.number);
}
}
Deserialized Employee...
Name: Abdul Ahad
Address: Wari, Dhaka
SSN: 0
Number: 1748

--

--