Quick revision
The short answer
Java serialization became a burden because it creates objects without running their constructors, so every serializable class has a hidden public constructor, and because it ties the wire format to private fields, so refactoring risks breaking stored data. Those properties made it the source of many serious remote code execution vulnerabilities through gadget chains, and so much code depends on it that it cannot simply be removed, only fenced in with filters and safer record rules. The lesson I take is to ask, before adopting any feature widely, how it could be retired and what it lets untrusted data do to our code.
What to remember
In this answer, we saw that serialization was a reasonable 1997 design for trusted RMI, built on transparency: constructing objects without constructors and writing private fields. That gave every serializable class a hidden constructor, tied stored data to private implementation, and enabled gadget-chain remote code execution. Because removal is impractical, the platform contained it with filters and constructor-based record serialization. The principles I take are to examine what a feature lets data do to code, plan how it could be retired before it spreads, and distrust convenience that bypasses the language's guarantees.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Java serialization became a burden because it creates objects without running their constructors, so every serializable class has a hidden public constructor, and because it ties the wire format to private fields, so refactoring risks breaking stored data. Those properties made it the source of many serious remote code execution vulnerabilities through gadget chains, and so much code depends on it that it cannot simply be removed, only fenced in with filters and safer record rules. The lesson I take is to ask, before adopting any feature widely, how it could be retired and what it lets untrusted data do to our code.
Why this question is asked
Every platform team eventually faces a feature that was convenient when it was added and costly for decades after. Java serialization is the textbook case, and the reasoning behind its history applies to frameworks, internal libraries and data formats we choose today. Interviewers ask this at principal level to hear whether we can turn a piece of history into principles that guide decisions. In this answer, we will see why serialization made sense in 1997, the three ways it went wrong, why it is so hard to remove and what the platform did instead, and then the principles I apply when evaluating new features, with an example.
1Why It Made Sense in 1997
Serialization arrived with Java 1.1 to support RMI, remote method invocation: calling a method on an object in another JVM as if it were local, and passing whole object graphs as arguments. To make that effortless, the design goal was transparency: implement a marker interface and any object could be turned into bytes and back, with no code to write. For a world of trusted Java programs talking to each other on a private network, that was a reasonable trade.
Transparency, though, meant that serialization worked around the language rather than through it. It had to construct objects without constructors and read and write private fields directly, because otherwise developers would have had to write code.
2Problem One: A Hidden Public Constructor
A constructor is where a class enforces its rules: amounts are not negative, ranges are ordered, references are copied. Deserialization skips the constructors of serializable classes and sets fields from the stream. So every class that implements Serializable gains a second, invisible way to create instances, one that accepts any field values the byte stream contains and is reachable by anyone who can send us bytes. Developers must remember to write readObject validation that mirrors their constructor, and most never did, because nothing in the class's source shows that this door exists.
3Problem Two: The Wire Format Is the Private Implementation
The default serialized form is the class's private fields, their names and types. That makes private implementation details part of a public, persistent contract. Renaming a field, changing its type, or splitting a class can break every stored session, cache entry and message written with the old version, sometimes months later when old data is read. Teams either froze classes they wanted to improve, or broke compatibility and found out in production. Encapsulation, one of the main promises of the language, quietly stopped applying to these classes.
4Problem Three: Gadget Chains and Remote Code Execution
The most expensive consequence was security. When a program deserializes untrusted bytes, the attacker chooses which classes are instantiated, from anything on the classpath, and with what field values. Some library classes run code during deserialization, in readObject, in hashCode when they are put in a HashMap, or in comparators during sorting. By chaining such classes, called gadgets, an attacker can make the deserialization of a carefully built stream execute arbitrary commands. From around 2015, published chains through widely used libraries such as Apache Commons Collections turned this into one of the most common sources of critical vulnerabilities in Java applications, servers and middleware.
Notice that none of the vulnerable code was in the application itself. Merely having a library on the classpath and accepting serialized input was enough. That is what makes this class of bug so hard to manage: risk came from the combination of a convenient feature and unrelated dependencies.
- Service reads untrusted bytes with readObjecte.g. a cookie, a message, an RMI call
- Stream names a library class on the classpaththe attacker picks the classes
- Its readObject or hashCode calls other objectsfields set by the attacker
- The chain reaches a method that runs codefor example invoking a method by reflection
- Command executes inside the serviceremote code execution
5 steps in order. Play them, or step through them one at a time.
5Why It Could Not Simply Be Removed
By the time the problems were obvious, serialization was everywhere: in application servers, caches, session replication, RMI, messaging systems and countless private file formats, and in billions of stored bytes. Removing it would break programs that have no practical way to change. So the platform took the path of containment.
Serialization filters, JEP 290 in Java 9 and backported to Java 8 updates, let an application allow or reject classes, and limit graph depth and size, before objects are created. JEP 415 in Java 17 added context-specific filters, so each use can have its own rules. Records, from Java 16, are serialized only through their canonical constructor, which closes the hidden-constructor door for them. And the platform's own direction is to reduce reliance on serialization rather than extend it. Each of these is useful, and each is a cost the platform continues to pay for a 1997 shortcut.
| Step | When | What it changed |
|---|---|---|
| Serialization filters (JEP 290) | Java 9, backported to 8 | allow or reject classes and limit graph size |
| Records serialized via constructor | Java 16 | no hidden constructor for records |
| Context-specific filters (JEP 415) | Java 17 | per-stream filters chosen by a factory |
| Guidance: do not deserialize untrusted data | ongoing | prefer JSON or protobuf with explicit schemas |
6The Principles I Take From It
The first principle is to ask what a feature lets data do to code. Serialization's real flaw was that input bytes chose which classes to instantiate and which code to run. Any feature where external data selects types, such as polymorphic JSON deserialization with type names in the payload, YAML loaders that construct arbitrary classes, or expression languages evaluated from configuration, deserves the same suspicion and should default to explicit allow-lists.
The second is to ask how a feature could be retired before adopting it widely. If a feature spreads into stored data and many codebases, its exit cost grows every month. We ask up front what the migration path would look like, keep usage behind a small number of internal interfaces, and prefer formats with explicit schemas that we own.
The third is to distrust transparency that bypasses the language's normal guarantees. Features that skip constructors, write private fields or generate behaviour invisibly are convenient on day one and expensive later, because the rules people rely on silently stop applying.
As an example, when a team proposed caching domain objects in a distributed cache using Java serialization because it needed no code, I would apply these questions: the cache holds data that other services read, so who can write to it? Stored entries outlive deployments, so how do we change classes? The answer would be a JSON or Protocol Buffers representation with explicit versioning, slightly more code now, far less risk later.
7Follow-up questions
Is it ever acceptable to use Java serialization today?
Within one trusted process or between tightly controlled components where no untrusted input can reach it, with a strict allow-list filter, and preferably for records. For data that crosses trust boundaries or is stored long term, an explicit format with a schema is the better choice.
How would you find existing uses in a large estate?
Search code for ObjectInputStream and readObject, check frameworks configured to use Java serialization (session replication, caches, RMI, some message converters), and run dependency scanners that flag known gadget libraries. Then add a global deserialization filter as an immediate safety net while uses are migrated.
What would you tell a product leader who asks why we spend time on this?
That it is one of the most exploited vulnerability classes in Java, that one exposed endpoint can mean full server compromise, and that the fix is mostly configuration and gradual migration, which is cheap compared with an incident.
8Conclusion
In this answer, we saw that serialization was a reasonable 1997 design for trusted RMI, built on transparency: constructing objects without constructors and writing private fields. That gave every serializable class a hidden constructor, tied stored data to private implementation, and enabled gadget-chain remote code execution. Because removal is impractical, the platform contained it with filters and constructor-based record serialization. The principles I take are to examine what a feature lets data do to code, plan how it could be retired before it spreads, and distrust convenience that bypasses the language's guarantees.
Open with
Serialization was designed in 1997 for RMI, where trusted JVMs passed object graphs to each other, and the goal was transparency: implement a marker interface and it just works.
Then, if they want more
To deliver that, it worked around the language. It creates objects without calling their constructors, so every serializable class effectively has a hidden public constructor that accepts any field values, and the validation in the real constructor never runs.
It uses private fields as the wire format, so refactoring a class can break stored sessions or messages, and encapsulation stops applying.
And the worst part: when a service deserializes untrusted bytes, the attacker chooses which classes on the classpath get instantiated, and by chaining library classes whose readObject or hashCode do things, the gadget chains, they can execute code, which from around 2015 became a major source of critical vulnerabilities.
It could not be removed because so much software and stored data depends on it, so the platform added filters in Java 9 and context-specific filters in 17, and made records go through their canonical constructor.
The principles I take from it: before adopting a feature widely, ask what it lets incoming data do to our code, ask how we would retire it once it spreads into stored data, and be wary of convenience that bypasses the language's own guarantees.
I apply that today to things like polymorphic JSON with type names in the payload or caching objects with Java serialization.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 16, Java 17, Java 8, Java 9
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Java serialization was added in 1997 for RMI and distributed objects. Explain why it...": https://scrivio-interview-library.netlify.app/java/serialization/3e387101c2/