Serialization in Java


Serialization in java is a mechanism of writing the state of an object into a byte stream.It is mainly used in Hibernate, RMI, JPA, EJB, JMS technologies.The reverse operation of serialization is called deserialization.

Advantage of Java Serialization

It is mainly used to travel object’s state on the network (known as marshaling). interface

Serializable is a marker interface (has no body). It is just used to “mark” java classes which support a certain capability.

It must be implemented by the class whose object you want to persist. Let’s see the example given below:

  1. import;
  2. public class Student implements Serializable{
  3.  int id;
  4.  String name;
  5.  public Student(int id, String name) {
  6. = id;
  7. = name;
  8.  }
  9. }


ObjectOutputStream class

An ObjectOutputStream is used to write primitive data types and Java objects to an OutputStream.Only objects that support the interface can be written to streams.


1) public ObjectOutputStream(OutputStream out) throws IOException {}creates an ObjectOutputStream that writes to the specified OutputStream.

Important Methods

1) public final void writeObject(Object obj) throws IOException {}writes the specified object to the ObjectOutputStream.
2) public void flush() throws IOException {}flushes the current output stream.
3) public void close() throws IOException {}closes the current output stream.


Example of Java Serialization

In this example, we are going to serialize the object of Student class. The writeObject() method of ObjectOutputStream class provides the functionality to serialize the object. We are saving the state of the object in the file named f.txt.


Deserilization in java

Deserialization is the process of reconstructing the object from the serialized state.It is the reverse operation of serialization.


ObjectInputStream class

An ObjectInputStream deserializes objects and primitive data written using an ObjectOutputStream.


1) public ObjectInputStream(InputStream in) throws IOException {}creates an ObjectInputStream that reads from the specified InputStream.

Important Methods

1) public final Object readObject() throws IOException, ClassNotFoundException{}reads an object from the input stream.
2) public void close() throws IOException {}closes ObjectInputStream.


Example of Java Deserialization


Serialization with Inheritance (IS-A Relationship)

If a class implements serializable then all its sub classes will also be serializable. Let’s see the example given below:


Now you can serialize the Student class object that extends the Person class which is Serializable.Parent class properties are inherited to subclasses so if parent class is Serializable, subclass would also be.

Serialization with Aggregation (HAS-A Relationship)

If a class has a reference of another class, all the references must be Serializable otherwise serialization process will not be performed. In such case, NotSerializableException is thrown at runtime.


Since Address is not Serializable, you can not serialize the instance of Student class.

Note: All the objects within an object must be Serializable.


Externalizable interface in java

The Externalizable interface provides the facility of writing the state of an object into a byte stream in compress format. It is not a marker interface.

The Externalizable interface provides two methods:

  • public void writeExternal(ObjectOutput out) throws IOException
  • public void readExternal(ObjectInput in) throws IOException

Serialization with static data member

If there is any static data member in a class, it will not be serialized because static is the part of class not object.


Serialization with array or collection

Rule: In case of array or collection, all the objects of array or collection must be serializable. If any object is not serialiizable, serialization will be failed./p>