Java Reference ยท I/O and Files
The Serializable interface
Reference for java.io.Serializable: the marker interface, serialVersionUID, transient fields, custom writeObject and readObject, Externalizable and records.
java.io.Serializable is a marker interface: it has no methods. A class that implements it tells the Java runtime that its instances may be converted to bytes by ObjectOutputStream and rebuilt by ObjectInputStream. A class that does not implement it is rejected with NotSerializableException.
Marking a class serializable is a public commitment: the field layout becomes part of your class's compatibility contract. This page covers the rules that decide what is written and how versions are matched. Prefer to keep serializable classes few and simple.
public class Account implements Serializable { @java.io.Serial private static final long serialVersionUID = 1L; private String owner; private transient String cachedLabel; }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public interface Serializable { } private static final long serialVersionUID = 1L; private void writeObject(ObjectOutputStream out) throws IOException private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException private void readObjectNoData() throws ObjectStreamException Object writeReplace() throws ObjectStreamException Object readResolve() throws ObjectStreamException
| Member | Description |
|---|---|
serialVersionUID |
A version number checked on reading. A mismatch throws InvalidClassException. |
transient |
Field modifier: excluded from serialization. |
writeObject / readObject |
Optional private hooks to customise how a class's own fields are written and read. Call defaultWriteObject / defaultReadObject first. |
writeReplace |
Lets an object substitute another object (a "serialization proxy") when written. |
readResolve |
Lets the read object be replaced, for example by the canonical singleton or enum constant. |
@Serial |
Annotation (Java 14+) that lets the compiler check these special members' signatures. |
Externalizable |
Sub-interface where you write and read every field yourself and need a public no-argument constructor. |
Usage notes
- Declare
serialVersionUIDexplicitly. Without it, Java computes one from the class shape, so adding a method can make old data unreadable. Compatible changes (adding a field) keep working when the ID stays fixed; removed or retyped fields do not. - Serializability is inherited, but state of a non-serializable superclass is not saved: the superclass needs an accessible no-argument constructor, which runs on reading (see the first example's output).
- Every field must be serializable, or marked
transient. Collections and strings are; threads, streams, and sockets are not. - Lambdas and inner classes: anonymous and inner (non-static) classes capture the outer instance, dragging it into the stream. Avoid serializing them.
- Singletons and enums: enums are always serialized by name and stay unique. A singleton class needs
readResolveor it is duplicated by deserialization. - Records: are serialized from their components and rebuilt through the canonical constructor, so validation runs.
serialVersionUIDis not matched for records. - Security: validate in
readObject, useObjectInputFilter, and never deserialize data you do not trust. - Design: the newer guidance is to avoid Java serialization for new data formats altogether.