Quick revision
The short answer
With Externalizable, the class writes and reads all of its state itself in writeExternal and readExternal, and deserialization creates the object with its public no-arg constructor before calling readExternal. With Serializable plus writeObject, Java still handles field metadata and the class hierarchy, and we only customise parts. Externalizable was a good choice for compact, fast formats when reflection-based serialization was slow, but it exposes a public way to reinitialise any object, prevents final fields, and leaves versioning entirely to us, so today a dedicated format such as Protobuf usually serves the same need better.
What to remember
In this answer, we saw that Externalizable hands the whole format to our code: writeExternal and readExternal write and read every value, and reading starts with the public no-arg constructor. It produced compact, fast streams because no field metadata is written automatically, which made it attractive when reflection was slow. Its risks are a public constructor and a public readExternal that let anyone reinitialise an object, no final fields, and fully manual versioning. Today a schema-based format such as Protobuf usually serves the same need better.
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.
With Externalizable, the class writes and reads all of its state itself in writeExternal and readExternal, and deserialization creates the object with its public no-arg constructor before calling readExternal. With Serializable plus writeObject, Java still handles field metadata and the class hierarchy, and we only customise parts. Externalizable was a good choice for compact, fast formats when reflection-based serialization was slow, but it exposes a public way to reinitialise any object, prevents final fields, and leaves versioning entirely to us, so today a dedicated format such as Protobuf usually serves the same need better.
Why this question is asked
Externalizable comes up in interviews about older systems: RMI services, home-grown caches and messaging layers written when every microsecond of serialization counted. Knowing why it existed, and why we rarely choose it now, shows we can reason about trade-offs instead of just syntax. In this answer, we will recap what Serializable writes by default, compare it with writeObject customisation and with Externalizable, look at the speed and size benefit, then go through the risks and what we would use today.
1A Quick Recap of What Serializable Writes
With plain Serializable, ObjectOutputStream writes a class descriptor for each class in the object's hierarchy: the class name, its serialVersionUID, and the name and type of every serializable field. Then it writes the field values, found through reflection. On the way back, ObjectInputStream matches fields by name, so an added field gets a default value and an unknown field is skipped. That metadata is what makes evolving a class possible without any code, and it is also what makes the format larger and slower than it needs to be.
A class can customise this with private writeObject and readObject methods. Usually they call defaultWriteObject() and defaultReadObject() to keep the automatic part, and then write extra data or validate. Java still writes the field descriptors and still handles each superclass separately.
2How Externalizable Works
Externalizable is an interface that extends Serializable and takes over completely. Java writes only the class name and serialVersionUID, then calls our writeExternal, and it is up to us to write every value, including the superclass's state. Reading is the mirror image, with one important difference from Serializable: the object is created by calling its public no-argument constructor, and only then is readExternal called to fill it in.
5 messages in order. Play them, or step through them one at a time.
Notice that the stream now contains no field names or types at all, just eight bytes of data after the class descriptor. If we forget the public no-arg constructor, writing works but reading fails with InvalidClassException: no valid constructor.
3When Externalizable Was a Good Choice
In the early 2000s, reflection was slow and the default mechanism was a well-known bottleneck in RMI and in clustered caches. Externalizable avoided reflection on every field and avoided writing field metadata, so for classes sent millions of times, such as small value objects in a trading or caching system, it gave measurable gains in both CPU and bytes. It also let a class choose a smart encoding, for example writing a large array of doubles as a single block, or writing a compact bitmap instead of many booleans.
So the honest summary is that Externalizable bought speed and compactness, at the price of doing all the work by hand. With Serializable plus writeObject, by contrast, we could keep Java's safety net for evolution and only optimise the expensive parts, which was often the better balance.
| Aspect | Serializable + writeObject | Externalizable |
|---|---|---|
| Field metadata in stream | yes, names and types | no, only what we write |
| Object creation on read | no constructor of the class runs | public no-arg constructor runs |
| final fields | allowed; defaultReadObject sets them | not possible |
| Superclass state | handled per class by Java | our code must write it |
| Adding a field later | automatic, default value on old data | manual version handling |
| Methods visibility | private writeObject/readObject | public writeExternal/readExternal |
4The Risks of Externalizable
The first risk is the public surface it forces on the class. readExternal is a public method, so any code holding a Point can call p.readExternal(someInput) and overwrite its state after it was constructed. The public no-arg constructor means anyone can create an empty, half-valid object. For an immutable value class this destroys the guarantees we usually want, which is also why the fields cannot be final.
The second risk is versioning. Because the stream has no field names, the reader must know exactly what the writer wrote, in which order. If release 2 adds a z coordinate, a release 1 reader will either fail or read garbage, unless we planned for it from the start by writing a version number first.
This works, but it is a hand-written schema that every developer must keep correct forever, and an old reader still cannot skip data it does not understand. Finally, Externalizable is still Java serialization: the stream names classes, so it carries the same deserialization security risks, and a validation mistake in readExternal is easy to make.
5What We Would Use Today
The reasons for Externalizable have mostly gone. Reflection and the default mechanism are much faster than they were, and when we truly need a compact, fast binary format, dedicated formats do it better. Protobuf, Avro or a similar schema-based format gives small messages, generated code in many languages, explicit rules for adding and removing fields, and readers that skip unknown fields safely. And because they never name Java classes in the data, reading them cannot instantiate arbitrary classes.
So when we meet Externalizable in an older system, we treat it as a format with a hidden, hand-written schema. If it crosses a process boundary, the long-term plan is usually to move it to a schema-based format, and in the meantime to make sure readExternal validates everything it reads.
6Follow-up questions
Is Externalizable still faster than Serializable today?
It can still be smaller and faster for some classes, because it writes no field metadata, but the difference is much smaller than it used to be, and a schema-based binary format is usually both faster and safer. We would measure with a benchmark on our real objects before choosing it for speed.
Can a record implement Externalizable?
Records are designed to be serialized through their components and canonical constructor, and they have no no-arg constructor or mutable fields for readExternal to fill. Externalizable does not fit them; if we need a custom binary form for record data, we write it with a separate codec or a schema-based format. Check your version's serialization specification for the exact rule.
Does Externalizable avoid deserialization attacks?
No. The stream still names the class to instantiate, and readExternal of any Externalizable class on the class path can run during reading. It needs the same deserialization filters and trust-boundary rules as Serializable.
7Conclusion
In this answer, we saw that Externalizable hands the whole format to our code: writeExternal and readExternal write and read every value, and reading starts with the public no-arg constructor. It produced compact, fast streams because no field metadata is written automatically, which made it attractive when reflection was slow. Its risks are a public constructor and a public readExternal that let anyone reinitialise an object, no final fields, and fully manual versioning. Today a schema-based format such as Protobuf usually serves the same need better.
Open with
Both are Java serialization, but they divide the work differently. With Serializable and a private writeObject, Java still writes the class descriptor with field names and types, handles each class in the hierarchy, and I just customise parts, usually calling defaultWriteObject and then adding or validating.
Then, if they want more
Reading does not run my class's constructor, and final fields still work. With Externalizable, Java writes only the class name and serialVersionUID and calls my writeExternal, where I write every value myself, superclass state included. On reading, it calls the public no-arg constructor and then my readExternal.
That was a good choice when reflection-based serialization was slow, for small value objects sent millions of times in RMI or cache clusters, because the stream has no field metadata and I control the encoding, so it is compact and fast. The risks are real though.
The public constructor and public readExternal mean anyone can create an empty object or reinitialise an existing one, so fields cannot be final and immutability is gone. Versioning is entirely manual; I have to write a version byte from day one and handle every old shape by hand. And it is still Java serialization, with the same security exposure.
Today, if I need a compact fast format, I would use Protobuf or Avro, which give explicit schemas and safe evolution.
About this answer
- Last updated
- 12 November 2026
- 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 "Compare Externalizable with Serializable plus writeObject. When was Externalizable a...": https://scrivio-interview-library.netlify.app/java/serialization/85464eed9e/