Software Training Institute in Chennai with 100% Placements – SLA Institute

Easy way to IT Job

Share on your Social Media

What Is The Concept Of Serialization In Java?

Published On: December 21, 2022

The term “serialization” describes the Java idea of encoding an object’s state as a sequence of bytes. All of the object’s data can be found in its byte stream. The serialization and deserialization of Java programs are common features in frameworks like Hibernate, JMS, JPA, and EJB.

Deserialization is the process of converting the serialized item back to its original memory location from its byte data type stream. These techniques are particularly advantageous since serialization can be performed on one JVM and de-serialization on another.

All you need to know about Java is covered in SLA’s Java training in Chennai. Join now and empower your career.

What Benefits Does Serialization Offer?

There are many advantages to serialization. Benefits include:

  • A tool for marshaling (traveling the state of an object on the network)
  • To keep or store an object’s current state
  • This is not dependent on the JVM.
  • Simple to comprehend and alter to fit your needs

Key Considerations for Java’s Serialization

Some requirements must be satisfied before an object may be serialized. There are a few more salient issues worth mentioning before continuing with the article. When working with serialization in Java, keep these considerations in mind.

  • There are no methods or data members in the serialization interface.
  • An object must adhere to the serializable interface in order to be serialized.
  • The serializable keyword should only be used if a class’s entirety can be serialized; otherwise, use the temporary keyword.
  • If the parent class already implements the Serializable interface, the child class is exempt from the requirement to do so.
  • Only non-static data members are saved by the serialization process; static data members and transitory data members are not saved.
  • The Serializable interface is automatically implemented by the String class and all wrapper classes.

Serialization in Java is a primary concept that you can learn in detail from Java training in Chennai.

A Guide to Object Serialization

Now that you have a solid understanding of what serialization is and why it’s important in Java, we can dive into the specifics of serializing an object. The serialization process is handled by the writeObject() function of the ObjectOutputStream class, whereas the deserialization process is handled by the readObject() method of the InputObjectStream class.

Syntax for the writeObject() method:

public final void writeObject(Object o) throws IO Exception

Syntax for the readObject() method:

public final Object readObject() throws IOException, ClassNotFoundException

An example can help you to understand how to do serialization and deserialization of an object in Java.

Illustration of Serialization in Java


public class SerializeDemo {

   public static void main(String [] args) {

      Employee e = new Employee(); = “Ravi Teja”;

      e.address = “Puja nagar, Amritsar”;

      e.SSN = 11112331;

      e.number = 111;

      try {

         FileOutputStream fileOut =

         new FileOutputStream(“/tmp/employee.ser”);

         ObjectOutputStream out = new ObjectOutputStream(fileOut);




         System.out.printf(“Serialized data is saved in /tmp/employee.ser”);

      } catch (IOException i) {





De-serializing an Object

Deserialization of the Employee object generated by SerializeDemo is illustrated below using DeserializeDemo. Learn as much as you can about the program and your best guess at its results.


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();



      } catch (IOException i) {



      } catch (ClassNotFoundException c) {

         System.out.println(“Employee class not found”);




      System.out.println(“Deserialized Employee…”);

      System.out.println(“Name: ” +;

      System.out.println(“Address: ” + e.address);

      System.out.println(“SSN: ” + e.SSN);

      System.out.println(“Number: ” + e.number);




Deserialized Employee…

Name: Ravi Teja

Address:Puja nagar, Amritsar

SSN: 0


Take note of the following essentials :

The readObject() method throws a ClassNotFoundException, which is attempted to be caught by the try/catch block. Object deserialization requires the Java Virtual Machine (JVM) to have access to the class’s bytecode. During object deserialization, the JVM will throw a ClassNotFoundException if it is unable to locate the specified class.

The value returned by readObject() is automatically transformed into an Employee reference.

Even though the SSN field had the value 11112333 before serialization, it was not written to the output stream because it is a volatile field. After deserialization, the Employee object’s SSN field contains a value of 0.

The Use of Serialization and Inheritance in Java (Is-A Relationship)

When a parent class implements the Serializable interface, the child classes do not need to do so as is noted in the points to note section. Let’s see how that point works in practice with this example of Java serialization in inheritance, shall we?

A Serialization Approach Using Aggregation in Java (Has-A Relationship)

Because the Address class does not implement the Serializable interface, the code that follows will not be able to serialize the Student object. This is because it attempted to use a reference from the Address class in order to serialize the Student class, but this was not successful. If you want to serialize an object that makes reference to another class, this means that all of the references themselves need to be serializable; otherwise, the serialization process will fail with a NotSerializableException.

Java Serialization Utilizing Static Data Members

In Java, the static data members are disregarded by the serialization process. Follow along with the example below to learn how to serialize an object and make use of a static data member. However, because serialization will not have any effect on the static data, the value of the variable can be changed before the variable is de-serialized.

What exactly is a transient keyword?

In Java, the keyword “transient” is considered a reserved word. You need to include the temporary keyword if you do not want any of the data members to be serialized in your program. You are going to apply the transitory keyword to two data members in the following line of code, and then you are going to display all of the data members’ values.

As can be seen, the temporary data members all return the default value, which in the case of integers is 0 and in the case of strings is either nothing or an empty string.


Everything you need to know about serialization in Java was covered in this article. You have the option of enrolling in SLA’s Java Certification Course training in Chennai if you would like to have a deeper understanding of more complex ideas. This course will assist you in gaining an understanding of numerous Java programming ideas and applying those concepts to Hibernate, Spring, and other frameworks.

Do you have any questions for us about the serialization process in Java? Leave them in the comments section of this article, and our expert staff members will respond as soon as possible.

Share on your Social Media

Just a minute!

If you have any questions that you did not find answers for, our counsellors are here to answer them. You can get all your queries answered before deciding to join SLA and move your career forward.

We are excited to get started with you

Give us your information and we will arange for a free call (at your convenience) with one of our counsellors. You can get all your queries answered before deciding to join SLA and move your career forward.