Serializable Interface in Java

In Java, the Serializable interface in Java is a marker interface that indicates that a class can be serialized. Serialization is the process of converting an object’s state into a byte stream. Which can then be transmitted over a network or stored in a persistent storage medium such as a file. The byte stream can later be used to recreate the object in memory.

How to make Java class serializable?

To make a Java class serializable, the class must implement the Serializable interface. This interface does not have any methods that need to be implemented. It simply serves as a flag to indicate that the class can be serialized.

Example

Here is an example of a serializable class in Java:

import java.io.Serializable;

public class Person implements Serializable {
    private static final long serialVersionUID = 1L;
    private String name;
    private int age;

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Serialize the object

To serialize an object of this class, we can use the ObjectOutputStream class.

Following is an example of how to serialize an object and write it to a file:

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

Person person = new Person("John", 30);

try (FileOutputStream fos = new FileOutputStream("person.ser");
     ObjectOutputStream oos = new ObjectOutputStream(fos)) {
    oos.writeObject(person);
} catch (IOException e) {
    e.printStackTrace();
}

Deserialize the object

To deserialize the object, we can use the ObjectInputStream class.

We can see an example of how to read the serialized object from a file and recreate it in memory in the following code:

import java.io.FileInputStream;
import java.io.ObjectInputStream;

Person person;

try (FileInputStream fis = new FileInputStream("person.ser");
     ObjectInputStream ois = new ObjectInputStream(fis)) {
    person = (Person) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
    e.printStackTrace();
}

It’s important to note that not all objects are serializable. For example, objects that contain references to non-serializable objects cannot be serialized. In these cases, we can use the transient keyword to exclude certain fields from the serialization process.

import java.io.Serializable;

public class Employee implements Serializable {
    private static final long serialVersionUID = 1L;
    private String name;
    private int age;
    private transient String password;  // exclude from serialization

    public Employee(String name, int age, String password) {
        this.name = name;
        this.age = age;
        this.password = password;
    }

    // getters and setters
}

Conclusion

The Serializable interface in Java is a powerful tool that allows us to easily serialize and deserialize objects. By implementing this interface and using the ObjectOutputStream and ObjectInputStream classes, we can easily store and retrieve objects from persistent storage or transmit them over a network. However, it’s important to note that not all objects are serializable and we may need to use the transient keyword to exclude certain fields from the serialization process.