← Back to Questions
Java

What is serialization with example?

Learn What is serialization with example? with simple explanations, real-time examples, interview tips and practical use cases.

What is Serialization in Java with Example?

Serialization in Java is the process of converting a Java object into a byte stream so that it can be stored in a file, transferred over a network, or saved in a database.

In simple words:

Serialization converts Java objects into a storable or transferable format.


Why Serialization is Important?

Enterprise applications often need to:

  • Save objects into files
  • Transfer objects across networks
  • Cache objects
  • Send objects between microservices
  • Store session data
  • Persist application state

Serialization Overview Diagram


Java Object

      |
      v

Serialization Process

      |
      v

Byte Stream

      |
      v

File / Network / Database


What Happens During Serialization?

  • Object state is converted into bytes
  • Bytes are written to file/network
  • Object structure gets preserved

Main Interface Used

Serializable

Important Point

Serializable is a marker interface.

It contains:

No methods

Why Marker Interface?

It tells JVM:

"This object can be serialized."


Serialization Flow


Class Implements Serializable

      |
      v

Object Created

      |
      v

ObjectOutputStream Converts Object

      |
      v

Byte Stream Generated


Basic Serialization Example

import java.io.*;

class Student
implements Serializable {

    int id;
    String name;

    Student(
        int id,
        String name
    ) {

        this.id = id;
        this.name = name;

    }

}

Serialization Code

FileOutputStream fos =

    new FileOutputStream(
        "student.ser"
    );

ObjectOutputStream oos =

    new ObjectOutputStream(
        fos
    );

Student s =

    new Student(
        101,
        "Naresh"
    );

oos.writeObject(s);

oos.close();

What Happens Internally?

  • Student object created
  • ObjectOutputStream converts object into byte stream
  • Bytes stored in file

Internal Serialization Flow


Student Object

      |
      v

ObjectOutputStream

      |
      v

Byte Conversion Happens

      |
      v

student.ser File Created


What is Deserialization?

Deserialization is the reverse process of serialization.


Deserialization Definition

Converting byte stream back into Java object.


Deserialization Example

FileInputStream fis =

    new FileInputStream(
        "student.ser"
    );

ObjectInputStream ois =

    new ObjectInputStream(
        fis
    );

Student s =

    (Student) ois.readObject();

System.out.println(
    s.id + " " + s.name
);

ois.close();

Output

101 Naresh

Deserialization Flow


Byte Stream Read

      |
      v

ObjectInputStream Processes Data

      |
      v

Original Object Recreated


What is serialVersionUID?

serialVersionUID is a unique version identifier for serialized classes.


Example

private static final long
serialVersionUID = 1L;

Why Important?

It prevents invalid deserialization when class structure changes.


serialVersionUID Flow


Serialized Object Saved

      |
      v

Class Version Changes

      |
      v

UID Compared During Deserialization

      |
      v

Mismatch Causes Exception


What Happens if serialVersionUID is Missing?

JVM automatically generates one.

But this can create compatibility problems later.


What is transient Keyword?

transient prevents fields from being serialized.


Example

class User
implements Serializable {

    String username;

    transient String password;

}

Why transient Used?

Sensitive data should not be serialized.


transient Flow


Object Serialized

      |
      v

transient Field Ignored

      |
      v

Sensitive Data Protected


Can Static Variables be Serialized?

No.

Because static variables belong to class, not object.


What if Parent Class is Not Serializable?

Child class can still be serialized if child implements Serializable.


What if Object Contains Non-Serializable Object?

Serialization fails with:

NotSerializableException

Example

java.io.NotSerializableException

Serialization in Banking Systems

Banking systems use serialization for:

  • Session management
  • Distributed transactions
  • Audit logging
  • Message queues
  • Transaction backup

Banking Flow


Transaction Object Created

      |
      v

Serialized Into Byte Stream

      |
      v

Stored in Queue/File

      |
      v

Recovered Later


Serialization in E-Commerce Systems

E-commerce platforms use serialization for:

  • Shopping cart storage
  • Distributed caching
  • Order processing
  • User sessions
  • Payment workflows

E-Commerce Flow


Shopping Cart Object

      |
      v

Serialized

      |
      v

Stored in Redis/Cache

      |
      v

Restored When User Returns


Serialization in Spring Boot

Spring Boot applications use serialization for:

  • REST API object transfer
  • Session replication
  • Distributed caching
  • Kafka messaging
  • Microservice communication

Spring Boot Example

class OrderEvent
implements Serializable {

}

Spring Boot Flow


REST Object Created

      |
      v

Serialized into JSON/Bytes

      |
      v

Transferred Between Services


Serialization in Microservices

Microservices architectures heavily use serialization for:

  • Kafka messaging
  • RabbitMQ events
  • Distributed caches
  • Inter-service communication
  • Cloud storage

Microservice Flow


Service Creates Event Object

      |
      v

Serialization Happens

      |
      v

Byte Stream Sent Through Kafka

      |
      v

Another Service Deserializes Object


Advantages of Serialization

  • Easy object persistence
  • Supports distributed systems
  • Useful for caching
  • Supports network communication
  • Simplifies object transfer

Disadvantages of Serialization

  • Performance overhead
  • Security risks
  • Version compatibility problems
  • Large object graphs increase memory usage

Difference Between Serialization and Deserialization

Feature Serialization Deserialization
Purpose Object → Byte Stream Byte Stream → Object
Main Class ObjectOutputStream ObjectInputStream
Direction Write Read

Common Interview Mistake

Many developers think serialization stores methods.

Actually:

  • Only object state (data) gets serialized.

Another Common Mistake

Many developers think transient prevents object creation.

Actually:

  • transient only skips serialization of specific fields.

Best Practices

  • Always define serialVersionUID
  • Use transient for sensitive data
  • Close streams properly
  • Use try-with-resources
  • Avoid serializing unnecessary large objects
  • Validate deserialized data carefully

Realtime Enterprise Example

Distributed Banking Transaction System


Transaction Object Created

      |
      v

Serialized into Byte Stream

      |
      v

Stored in Kafka Queue

      |
      v

Remote Service Reads Message

      |
      v

Object Deserialized

      |
      v

Transaction Continues


Related Learning Topics


Professional Interview Answer

Serialization in Java is the process of converting a Java object into a byte stream so that it can be stored in files, transferred across networks, cached in distributed systems, or persisted for later recovery. It is implemented using the Serializable marker interface along with classes such as ObjectOutputStream and ObjectInputStream. During serialization, the object state is converted into bytes, while deserialization reconstructs the original object from the byte stream. Enterprise applications, Spring Boot systems, banking platforms, distributed microservices, REST APIs, Kafka-based architectures, e-commerce systems, and cloud-native applications heavily use serialization for distributed caching, session replication, event streaming, inter-service communication, transaction recovery, and object persistence. Important concepts related to serialization include serialVersionUID, transient fields, object streams, version compatibility, and secure deserialization practices.


Frequently Asked Questions

What is serialization in Java?

Serialization is converting Java objects into byte streams.

Which interface is required for serialization?

Serializable interface.

What is deserialization?

Converting byte streams back into Java objects.

Why is serialVersionUID important?

It maintains version compatibility during deserialization.

Why is transient keyword used?

To prevent sensitive or unnecessary fields from being serialized.

Why this Java question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.

About the Author

Naresh Kumar is a Senior Java Backend Engineer with experience building enterprise applications using Java, Spring Boot, Microservices, Docker, Kubernetes and Cloud technologies.