Learn Java
import com.learnjava.filehandling.Serialization;

Serialization in Java

@Concept

Serialization converts a Java object's state into a byte stream that can be saved to a file, sent over a network, or stored anywhere bytes can go; deserialization reverses that process, reconstructing an equivalent object from a previously-serialized byte stream. A class opts into this by implementing the Serializable interface, a marker interface that declares no methods at all — it just signals to the JVM that objects of this class are allowed to be serialized.

Every field in a serializable class gets included in the byte stream by default unless it's marked transient, which explicitly excludes that one field — useful for data that shouldn't be persisted, like a cached value, or genuinely can't be, like a database connection. A serialVersionUID field, though technically optional, is strongly recommended, since it lets the JVM verify a deserialized byte stream's class version still matches the current class definition, rather than failing unpredictably if the class has changed since that object was originally serialized.

@HowItWorks
1

Writing the object out

Converts an object's state into a byte stream that can be saved or transmitted.

2

Deserialization

Reverses the process, reconstructing an equivalent object from that byte stream.

3

The Serializable interface

A marker interface with no methods at all, just an opt-in signal that a class's objects are allowed to be serialized.

4

transient fields

Explicitly excluded from the byte stream, for data that shouldn't be persisted or genuinely can't be, like a live connection.

5

serialVersionUID

A recommended field that lets the JVM verify a deserialized stream's class version still matches the current class definition.

@Example
import java.io.*;
 
public class SerializationDemo {
static class User implements Serializable {
String name;
transient String sessionToken;
 
User(String name, String sessionToken) {
this.name = name;
this.sessionToken = sessionToken;
}
}
 
public static void main(String[] args) throws Exception {
User original = new User("Ana", "secret-token");
 
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
new ObjectOutputStream(byteStream).writeObject(original);
 
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(byteStream.toByteArray()));
User restored = (User) in.readObject();
 
System.out.println("Name: " + restored.name);
System.out.println("Token: " + restored.sessionToken);
}
}
Output
Name: Ana
Token: null
1User implements Serializable, opting into the ability to be converted to a byte stream at all.
2Writing original to an ObjectOutputStream serializes its state, but sessionToken, marked transient, is deliberately skipped.
3Reading that byte stream back with an ObjectInputStream reconstructs a new User object, restored, with name correctly restored.
4sessionToken comes back as null on restored, since a transient field is never actually written to the byte stream in the first place.
@UseCases
Saving an object's state to a file so it can be reloaded exactly as it was in a later run
Sending a Java object across a network connection between two JVMs
Excluding a sensitive or non-persistable field, like a password or live connection, from serialization with transient
Using serialVersionUID to detect when a deserialized object's class has changed incompatibly since it was saved

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.