Serializable Interface in Java

Updated:

By

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

Following 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;
    }
}

What is Serialization and Deserialization in Java?

Serialization is the process of converting the state of an object into a byte stream. This byte stream can then be saved to a file, sent over a network, or stored in a database. And Deserialization is kind of the opposite of Serialization. This means, converting back to a Java object from a byte stream.

Serialize object in Java

To serialize 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.IOException;
import java.io.ObjectOutputStream;

public class SerializeObjectDemo {
	public static void main(String[] args) {
		Person person = new Person("Virat Kohli", 30);

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

Deserialize object in Java

To deserialize object in Java, 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.IOException;
import java.io.ObjectInputStream;

public class DeserializeObjectDemo {
	public static void main(String[] args) {
		Person person;

		try (FileInputStream fis = new FileInputStream("Person.txt");
				ObjectInputStream ois = new ObjectInputStream(fis)) {
			person = (Person) ois.readObject();
			System.out.println("Name: "+person.getName());
		} catch (IOException | ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}

Output:

Name: Virat Kohli

Exclude from serialization in Java

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
}

In this Employee class, when we try to serialize the field password will be excluded and when we deserialize and print the value of that field it will be null.

Homework

You can try to use the Employee class to serialize and deserialize and let me know in the comment section below what will be the output.


Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments