Java Tutorial

Serialization

Saving objects to binary data with Serializable and ObjectOutputStream

What is Serialization?

In Java, Serialization is the process of converting an object's state (its data) into a stream of Bytes. This allows you to "flatten" an object so it can be saved to a file, sent over a network, or stored in a database.
Deserialization is the opposite: rebuilding the object from those bytes!
java
import java.io.*;

// Just mark it with the interface! (No code needed!)
public class User implements Serializable {
    public String name;
    public int score;
}

Comparisons: The Two Essential Points

  • Serializable: A "Marker Interface" (No methods!). Telling the JVM it's safe to save this object.
  • transient: A keyword that tells the JVM to SKIP a variable during serialization. (Use this for passwords or temporary data!).
java
public class User implements Serializable {
  public String name;
  public transient String password; // This WON'T be saved to disk!
}

How to Save and Load Objects

Use ObjectOutputStream to save (Write) and ObjectInputStream to load (Read) your objects.
java
User user = new User("Alpha", 100);

// SAVE!
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("user.bin"))) {
    out.writeObject(user);
}

// LOAD!
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("user.bin"))) {
  User loadedUser = (User) in.readObject();
  System.out.println("Loaded: " + loadedUser.name);
}

The serialVersionUID

Wait! If you change your class (e.g., add a new field) after saving it, Java might fail to load it back. To prevent this, always define a serialVersionUID. This is a "Version Number" for your class.
java
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    // ... data ...
}

Summary

TermDescription
SerializableInterface that allows an object to be saved
TransientVariable that should NOT be serialized
SIDVersion number for your class (serialVersionUID)
StreamBinary data representing the object state
MarkerInterface with NO methods (Just a flag for the JVM!)
Best ForSaving game states, caching data, network objects
SafetyPrevents "InvalidClassException" with a stable SID
CleanYour object becomes its own persistent data format
Securitytransient prevents sensitive data leaks!
ManualUse readObject()/writeObject() for custom logic
LegacySerialization is powerful but slow and sometimes risky!
NioCombine with Path and Files for modern storage
FrameworkUsed internally by standard Java tools like JPA/RMI
ExternalUse Externalizable interface for TOTAL control
MemorySerialization uses buffers to minimize disk hits
HierarchyIf a parent is Serializable, the child is TOO!
SafetyOnly Serialized data is safe from "Garbage Collection"
StandardEstablished and trusted worldwide for 25+ years
EfficiencyBinary serialization is faster than human-readable JSON
ConstraintNon-serializable parents MUST have a no-args constructor!
StaticRegular static variables are NOT serialized!
BlockUse "Serialiazation Filters" (Java 9+) for better security
FutureModern Java often uses JSON/Protobuf for networking