Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 47

Serialization in Java

In this lesson: Serialize and deserialize objects in Java with Serializable, ObjectOutputStream and transient — with output. In Hindi & English.

What is serialization

Serialization turns an object into a stream of bytes so you can save it to a file, store it, or send it over a network. Deserialization does the reverse — rebuilding the object from those bytes. This lets an object outlive a single program run.

💡 Serialize = object → bytes (to save/send). Deserialize = bytes → object (to restore).

The Serializable interface

import java.io.Serializable;

class Student implements Serializable {
    int id;
    String name;
    Student(int id, String name) { this.id = id; this.name = name; }
}

A class must implement Serializable to be serialized. It is a marker interface — it has no methods; it just tells Java the object is allowed to be turned into bytes.

Serializing an object

import java.io.*;

Student s = new Student(1, "Aman");
ObjectOutputStream out =
    new ObjectOutputStream(new FileOutputStream("student.ser"));
out.writeObject(s);        // object -> bytes -> file
out.close();
System.out.println("Saved");
Saved

An ObjectOutputStream writes the object to a file as bytes with writeObject. The object is now stored on disk and can be restored later.

Deserializing it back

ObjectInputStream in =
    new ObjectInputStream(new FileInputStream("student.ser"));
Student s = (Student) in.readObject();   // bytes -> object
in.close();
System.out.println(s.name);
Aman

ObjectInputStream reads the bytes back and readObject rebuilds the object. You cast the result to the original type to use it again.

The transient keyword

class User implements Serializable {
    String name;
    transient String password;   // NOT saved
}

A transient field is skipped during serialization, so its value is not written to the file. Use it for sensitive data like passwords, or for values you can recompute after loading.

Common uses

  • Saving state — store objects to a file to reload later.
  • Networking — send objects between programs or over the internet.
  • Caching — keep computed objects for reuse.

Serialization is the standard way to persist or transmit whole Java objects.

Common mistakes

  • Not implementing Serializable. Causes NotSerializableException.
  • Forgetting to close streams. Close them, or use try-with-resources.
  • Wrong cast on readObject. Cast to the exact original type.
  • Serializing sensitive data. Mark passwords and secrets transient.

Summary

  • Serialization turns an object into bytes to save or send.
  • Deserialization rebuilds the object from those bytes.
  • The class must implement the marker interface Serializable.
  • Use ObjectOutputStream/ObjectInputStream to write/read.
  • Mark fields transient to skip them during serialization.

Next, handle file and I/O errors safely with Java exception handling, or manage memory with garbage collection.

Frequently Asked Questions

What is serialization in Java?
Serialization is converting an object into a stream of bytes so it can be saved to a file, stored, or sent over a network. Deserialization is the reverse — turning those bytes back into an object. It lets objects persist beyond a single run.
How do I make a class serializable?
Make the class implement the Serializable interface. It has no methods to write — it is a marker that tells Java the object can be serialized. Without it, serialization throws a NotSerializableException.
What is the transient keyword?
A field marked transient is skipped during serialization, so its value is not saved. Use it for data you do not want stored, like passwords or values that can be recomputed.
What is deserialization?
Deserialization reads the saved bytes and rebuilds the original object in memory. You use an ObjectInputStream and readObject(), then cast the result back to the original type.
What is serialVersionUID?
serialVersionUID is a version number for a serializable class. It checks that the saved bytes match the class version when deserializing. Setting it yourself avoids errors when the class changes slightly.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicSingleton Pattern Your progress: 47 / 61 (77%) · Lesson 48 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free