Serialization
Java serialization turns an object graph into a byte stream and back. A class opts in with implements Serializable. Prefer explicit formats (JSON, protobuf) for new work.
Detailed Explanation
ObjectOutputStream.writeObject walks an object, its fields, and the objects those fields refer to, and writes a binary encoding. ObjectInputStream.readObject rebuilds the graph. The class must implement Serializable. transient fields are skipped. A serialVersionUID is a version stamp; if you do not declare one, the compiler generates one from the class shape, and any field change makes old bytes unreadable.
The feature exists so the JDK could send objects over RMI and so some frameworks could persist sessions. It is a deep, implicit contract. Private fields become part of the on-disk form. Changing a type, a superclass, or a field name can break stored bytes. Custom writeObject/readObject, readResolve, and writeReplace let you control the mapping, and they are easy to get wrong.
Security is the reason new code should not use it. readObject runs constructors incompletely and can invoke unexpected class initializers. A crafted byte stream has been a remote code execution vector many times. The JDK now filters incoming classes (ObjectInputFilter, JEP 290). If you must deserialize, allow-list types and never deserialize bytes you do not trust.
Records serialize through their components. Enums serialize by name. Inner classes serialize the hidden outer instance, which leaks more graph than you meant. Proxies and lambdas have special forms. Serializable on an API type is a forever promise.
You reach for Java serialization only when an existing API forces it (some HttpSession attributes, old JMS messages). You reach for JSON, Jackson/Jsonb, protobuf, or a database row for anything you design today. Those formats are explicit, filterable, and language-neutral.
The cost of Serializable is a public binary API you did not intend to publish, plus a large attack surface. Mark types Serializable only when you must, declare serialVersionUID explicitly, keep the graph small, and treat incoming bytes as hostile.
Code Examples
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class RoundTrip implements Serializable {
private static final long serialVersionUID = 1L;
private final String name;
public RoundTrip(String name) {
this.name = name;
}
public static void main(String[] args) throws Exception {
RoundTrip original = new RoundTrip("Ira");
ByteArrayOutputStream raw = new ByteArrayOutputStream();
try (ObjectOutputStream out = new ObjectOutputStream(raw)) {
out.writeObject(original);
}
try (ObjectInputStream in = new ObjectInputStream(
new ByteArrayInputStream(raw.toByteArray()))) {
RoundTrip copy = (RoundTrip) in.readObject();
System.out.println(copy.name); // prints: Ira
}
}
}import java.io.Serializable;
public class Secret implements Serializable {
private static final long serialVersionUID = 1L;
private transient String token; // not written to the byte stream
public Secret(String token) {
this.token = token;
}
}Common Gotchas
Deserializing untrusted bytes is a security bug
use an `ObjectInputFilter` allow-list, or do not use Java serialization.
Missing `serialVersionUID` makes any class change incompatible
declare it and bump it when you intend a break.
Inner-class serialization writes the outer instance
unexpected objects (and secrets) ride along.
`transient` fields come back as defaults (`null`, `0`)
restore them in `readObject` if they are required.