Serialization and Deserialization Example

author-image   By Dhiraj,   21 October, 2016 3K

Serialization is a mechanism provided by java to save the state of one or more objects.During serialization, an object is represented as a sequence of bytes which holds the object and all of its instance variables.And once the object is serialized, whenever required it can be again deserialized to retain the object again with the exact same state in memory.Serialization and deserialization is JVM independent and hence an object can be serialized at one platform and the same object can be retained at another platform with the same state.

Better to know

1. Java serialization mechanism takes care of serializing complete object graph. For example if Department class has any instance varible of let say Employee than Employee class object will also be serialized given that Employee class also implements Serializable.If not, we get a runtime exception as

2. Again if you do not want to serilize Employee object or you don't have control over the Employee Object then it must be declared as a Transient variable during its declaration in Department class.

3. If a superclass is Serializable, then all subclasses of that class automatically implements Serializable implicitly.

 Other Interesting Posts
Hello World Java Program Breakup
Convert HashMap to List in Java
Sorting HashMap by Key and Value in Java
Why wait(),notify(),notifyAll() defined in object class in Java
How to use Comparable and Comparator in Java with Example

ObjectOutputStream and ObjectInputStream

ObjectOutputStream and ObjectInputStream are considered to be higher classes in the packages as we already know the lower-level classes , such as and mechanism of the basic serialization happens with just two methods one to serialize objects and write them to a stream, and a second to read the stream and deserialize objects.

ObjectOutputStream.writeObject()      //serialze and write
ObjectInputStream.readObject()        //read and deserialize

Here the Department class implements the Serializable interface. Serializable is a marker interface;it has no methods to impement.It is only used to indicate something to compiler or JVM.

public class Department implements Serializable {}

Following is the implementation of class which will really serialize and deserialize the Department Object.The invocation of writeObject() will serialize the object and then also writes the serialized object to a file. Similarly, invocation of readObject() returns an Object, that's why we need to cast it to the Department object.
public class SerializeDept {
	public static void main(String[] args) {
		Department dept = new Department();
		try {
			FileOutputStream fs = new FileOutputStream("serializedDept.txt");
			ObjectOutputStream os = new ObjectOutputStream(fs);
		}	catch(Exception e) { e.printStackTrace();}
		try {
			FileInputStream fis = new FileInputStream("serializedDept.txt");
			ObjectInputStream ois = new ObjectInputStream(fis);
			c = (Cat) ois.readObject();
		}   catch (Exception e) { e.printStackTrace(); }

Deserialization Steps

1. All instance variables are assigned to their default values.

2. The constructor is invoked, which immdiately invokes the superclass constructor.

3. Instance variables that are instantiated as part of their declaration are assigned their initial value.

4. The constructor completes.


I hope this article served you that you were looking for. If you have anything that you want to add or share then please share it below in the comment section.

If You Appreciate This, You Can Consider:

  • Like us at: Facebook or follow us at Twitter
  • Share this article on social media or with your teammates.
  • We are thankful for your never ending support.

About The Author


I am an energetic professional who enjoys the challenges involved in working with people and resolving real-time problems. Technical expertise in building highly scalable, distributed and self-healing cloud applications. Technical Skills: Java/J2EE, Spring Framework, Hibernate, Angular, Reactive Programming, Microservices, Rest APIs, Kafka, ELK, etc.

Further Reading on Core Java

1 Why Wait Notify Notifyall Defined In Object Class

2 Comparable And Comparator In Java With Example

3 Convert Hashmap To List Java

4 Sorting Hashmap By Key And Value

5 Hello World Java Program Breakup