← Back to Questions
Java

What is serialization in Java?

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

What is Serialization in Java?

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

In simple words:

Serialization converts Java objects into bytes, and Deserialization converts bytes back into objects.


Why Serialization is Important?

Serialization is used for:

  • Saving objects into files
  • Sending objects over network
  • Caching objects
  • Distributed systems communication
  • Session management
  • Microservices communication

Serialization Overview Diagram


Java Object

      |
      v

Serialization

      |
      v

Byte Stream

      |
      v

File / Network / Database


Deserialization Flow


Byte Stream

      |
      v

Deserialization

      |
      v

Java Object Restored


What is Serializable Interface?

Java provides:

java.io.Serializable

marker interface for serialization.


Why Called Marker Interface?

Because it contains:

No Methods

Basic Serialization Example

import java.io.*;

class Employee
implements Serializable {

    int id;
    String name;

}

Serialization Example

FileOutputStream fos =
    new FileOutputStream(
        "employee.ser"
    );

ObjectOutputStream oos =
    new ObjectOutputStream(fos);

Employee emp =
    new Employee();

oos.writeObject(emp);

What Happens Internally?

  • Object converted into byte stream
  • Byte stream written into file
  • Object state preserved

Serialization Internal Flow


Java Object

      |
      v

ObjectOutputStream

      |
      v

Byte Conversion

      |
      v

Stored in File


Deserialization Example

FileInputStream fis =
    new FileInputStream(
        "employee.ser"
    );

ObjectInputStream ois =
    new ObjectInputStream(fis);

Employee emp =
    (Employee) ois.readObject();

Deserialization Internal Flow


Byte Stream Read

      |
      v

ObjectInputStream

      |
      v

Bytes Converted to Object

      |
      v

Original Object Restored


What Data Gets Serialized?

  • Object state
  • Instance variables
  • Non-transient variables

What Does NOT Get Serialized?

  • Static variables
  • Transient variables
  • Methods
  • Constructors

Why Static Variables Are Not Serialized?

Because static variables belong to class, not object.


What is transient Keyword?

transient prevents variable serialization.


Example

class User
implements Serializable {

    String username;

    transient String password;

}

Why Important?

Sensitive data like passwords should not be serialized.


transient Flow


Object Serialization

      |
      +-------> username Saved

      |
      +-------> password Ignored


What is serialVersionUID?

serialVersionUID is a unique version identifier for serialized classes.


Example

private static final long
serialVersionUID = 1L;

Why serialVersionUID is Important?

It ensures compatibility during deserialization.


Problem Without serialVersionUID

Class modifications may break deserialization.


InvalidClassException Example

java.io.InvalidClassException

Serialization with Inheritance

Child class objects can also be serialized.


Example

class Person
implements Serializable {

}

class Employee
extends Person {

}

What if Parent is Not Serializable?

Parent default constructor executes during deserialization.


Difference Between Serialization and Deserialization

Feature Serialization Deserialization
Purpose Object → Bytes Bytes → Object
Stream Used ObjectOutputStream ObjectInputStream
Direction Write Read
Storage File/Network Memory Object

What is Externalization?

Externalization gives complete control over serialization process.


Interface Used

Externalizable

Difference Between Serializable and Externalizable

Feature Serializable Externalizable
Control Automatic Manual
Methods Required No writeExternal/readExternal
Performance Slower Faster
Complexity Simple Advanced

Serialization in Banking Systems

Banking applications use serialization for:

  • Transaction transfer
  • Distributed caching
  • Session replication
  • Audit logging

Example Scenario


Transaction Object

      |
      v

Serialized

      |
      v

Sent Across Banking Servers


Serialization in E-Commerce Systems

E-commerce systems use serialization for:

  • Shopping cart persistence
  • Distributed sessions
  • Order processing
  • Cache storage

Serialization in Spring Boot

Spring Boot applications use serialization for:

  • REST API communication
  • Session storage
  • Redis caching
  • Kafka messaging

Spring Boot Example

@RedisHash
class Product {

}

Objects serialized before storing in Redis.


Serialization in Microservices

Microservices architectures heavily use serialization for:

  • JSON conversion
  • Kafka events
  • RabbitMQ messaging
  • Distributed communication

Microservice Communication Flow


Service A Object

      |
      v

Serialized as JSON

      |
      v

Kafka / REST / RabbitMQ

      |
      v

Service B Deserializes Object


Java Serialization vs JSON Serialization

Feature Java Serialization JSON Serialization
Format Binary Text
Readable No Yes
Performance Faster Human-readable
Used In Java Systems REST APIs

Security Risks of Serialization

Improper deserialization may lead to security vulnerabilities.


Risks

  • Remote code execution
  • Object injection attacks
  • Malicious payload execution

Security Best Practices

  • Avoid deserializing untrusted data
  • Use validation
  • Use JSON/XML safely
  • Limit allowed classes

Advantages of Serialization

  • Easy object persistence
  • Supports distributed systems
  • Enables network communication
  • Useful for caching and sessions

Disadvantages of Serialization

  • Performance overhead
  • Security risks
  • Version compatibility issues
  • Binary format not human-readable

Common Interview Mistake

Many developers think static variables are serialized.

Actually:

  • Static variables belong to class, not object.

Another Common Mistake

Many developers think constructors run during deserialization.

Actually:

  • Constructors are NOT called during deserialization for Serializable classes.

Best Practices

  • Always define serialVersionUID
  • Use transient for sensitive data
  • Avoid Java serialization for public APIs
  • Prefer JSON for microservices communication
  • Validate deserialized data carefully

Realtime Enterprise Example

Kafka Event Processing


Order Object Created

      |
      v

Serialized into JSON

      |
      v

Kafka Topic

      |
      v

Another Service Deserializes Event


Related Learning Topics


Professional Interview Answer

Serialization in Java is the process of converting an object into a byte stream so that it can be stored in files, transferred over networks, or persisted in distributed systems. Java provides the Serializable marker interface to enable automatic serialization using ObjectOutputStream and ObjectInputStream. Deserialization restores the original object from the byte stream. Serialization is widely used in enterprise applications, Spring Boot systems, banking platforms, caching systems, Kafka messaging, cloud-native microservices, session replication, and distributed architectures. Important concepts include transient variables, serialVersionUID, Externalizable interface, and secure deserialization practices to prevent security vulnerabilities and compatibility issues.


Frequently Asked Questions

What is serialization in Java?

Serialization is the process of converting an object into a byte stream.

What is deserialization?

Deserialization converts byte stream back into original Java object.

Which interface is used for serialization?

Serializable interface is used for serialization.

Why is serialVersionUID important?

It ensures compatibility during deserialization.

Are static variables serialized?

No, static variables are not 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.