Quick revision
The short answer
ObjectOutputStream.writeObject walks an object graph and writes, for every object, a description of its class and the values of its non-transient, non-static fields, remembering objects it has already written so shared references and cycles survive. ObjectInputStream.readObject rebuilds the graph without calling the constructors of serializable classes. A class must implement the marker interface Serializable, every object it refers to must be serializable too, and fields of a non-serializable superclass are not saved but re-initialised by that superclass's no-arg constructor.
What to remember
In this answer, we saw that ObjectOutputStream writes a graph of objects with class descriptors and the values of their non-static, non-transient fields, using handles so shared objects and cycles survive, while ObjectInputStream rebuilds it. A class opts in with the Serializable marker interface, and every referenced object must be serializable or NotSerializableException is thrown. On reading, constructors of serializable classes do not run; only the first non-serializable superclass's no-arg constructor does, so that superclass's fields are not saved.
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.
ObjectOutputStream.writeObject walks an object graph and writes, for every object, a description of its class and the values of its non-transient, non-static fields, remembering objects it has already written so shared references and cycles survive. ObjectInputStream.readObject rebuilds the graph without calling the constructors of serializable classes. A class must implement the marker interface Serializable, every object it refers to must be serializable too, and fields of a non-serializable superclass are not saved but re-initialised by that superclass's no-arg constructor.
Why this question is asked
Java serialization is old and is no longer the recommended way to exchange data, but it is still inside many systems: HTTP session replication, caches, message payloads, RMI and legacy file formats. Interviewers ask about its mechanics because the rules, especially around constructors and superclasses, are surprising, and because those surprises explain both its bugs and its security problems. In this answer, we will make a class serializable and round-trip it, see what is written and what is skipped, see how shared references and cycles are kept, look at the superclass rule with the constructor calls printed, and finish with NotSerializableException.
1A Quick Recap: Writing and Reading an Object
Serialization turns an object into bytes, and deserialization turns those bytes back into an equal object, possibly in another JVM. ObjectOutputStream does the writing and ObjectInputStream the reading. A class opts in by implementing java.io.Serializable, a marker interface: it has no methods, it simply tells the JVM that writing this class is allowed.
The stream starts with the magic number 0xACED and a version, then contains a class descriptor, the class name, its serialVersionUID and the names and types of its fields, followed by the field values. Records are a special case: they are always rebuilt through their canonical constructor, which makes them the safest kind of class to serialize.
2What Gets Written and What Does Not
For an ordinary class, writeObject saves every field that is neither static nor transient. Static fields belong to the class, not the object, so they are never written. transient marks a field we deliberately do not want saved, such as a cache or a derived value; after reading, it has its default value, null, 0 or false.
Notice that copy.token is null, not "x9", even though the field has an initialiser. The reason is in the next section: for a serializable class, deserialization does not run constructors or field initialisers at all.
3References, Shared Objects and Cycles
writeObject writes a graph, not a single object. As it goes, it keeps a table of every object already written in this stream. When it meets the same object again, it writes a short back reference, a handle, instead of a second copy. On reading, the handle points to the same rebuilt object. That keeps shared objects shared and makes cycles safe instead of infinite.
The table also explains a common surprise: if we write the same object twice to one stream after changing it, the second write is only a handle, so the change is not saved. ObjectOutputStream.reset() or writeUnshared() are the tools for that case.
4Constructors and Non-Serializable Superclasses
Here is the rule that surprises people most. When reading an object, the JVM finds the closest superclass that is not serializable, often Object, and calls that class's no-arg constructor. The constructors of the serializable classes below it are not called; their fields are set directly from the stream. So fields of a non-serializable superclass are never written, and on reading they get whatever that superclass's no-arg constructor gives them.
6 messages in order. Play them, or step through them one at a time.
Only the first non-serializable superclass's constructor runs; serializable classes get their fields straight from the bytes.
So the superclass must have a no-arg constructor that the subclass can access, or reading fails with InvalidClassException: no valid constructor. And if the superclass state matters, the subclass has to save and restore it itself with writeObject and readObject methods. Skipping constructors is also why serialization is risky: any validation a constructor does is bypassed, so a crafted stream can create objects that the constructor would have rejected.
5NotSerializableException and How to Read It
Every object reachable through the non-transient fields must be serializable. If one is not, the whole writeObject call fails with NotSerializableException, and the message names the offending class.
The fix depends on what the field is. A logger, a connection or a thread is not state, so we mark it transient or make it static. A value object that should travel needs to implement Serializable. The exception is thrown at write time, so a test that round-trips each serializable class catches it before production does.
6Follow-up questions
What does serialVersionUID have to do with this?
It is the version number written in the class descriptor. On reading, it must match the local class's value, otherwise the read fails with InvalidClassException, so classes meant to be read back later should declare it explicitly.
Why do records behave differently?
Records are serialized as their components and always rebuilt through the canonical constructor, so any validation in a compact constructor runs on deserialization too. Custom writeObject and readObject methods are ignored for records.
Is Serializable inherited?
Yes. If a superclass implements Serializable, every subclass is serializable too, and all their fields are written unless they are transient. That is one reason to be careful before making a widely used base class serializable.
7Conclusion
In this answer, we saw that ObjectOutputStream writes a graph of objects with class descriptors and the values of their non-static, non-transient fields, using handles so shared objects and cycles survive, while ObjectInputStream rebuilds it. A class opts in with the Serializable marker interface, and every referenced object must be serializable or NotSerializableException is thrown. On reading, constructors of serializable classes do not run; only the first non-serializable superclass's no-arg constructor does, so that superclass's fields are not saved.
Open with
A class becomes serializable by implementing Serializable, which is a marker interface with no methods. When I call writeObject on an ObjectOutputStream, it writes a class descriptor, the class name, serialVersionUID and field names and types, and then the values of all fields that are not static and not transient, and it recurses into the objects those fields refer to.
Then, if they want more
It keeps a table of objects already written, so if the same object appears twice it writes a back reference, which means shared objects stay shared and cycles do not loop forever.
Every object reached that way must be serializable as well, otherwise the write fails with NotSerializableException naming the class, which is typically a logger or connection that should be transient. Reading is where the surprises are. For serializable classes, readObject does not call their constructors; it allocates the object and sets the fields straight from the stream.
The JVM only calls the no-arg constructor of the first superclass that is not serializable, so that superclass needs an accessible no-arg constructor, and its fields are not saved at all, they just get whatever that constructor sets. That bypassing of constructors is also why serialization can create objects that break invariants. Records are the exception: they are rebuilt through their canonical constructor.
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 "How does Java serialization work with ObjectOutputStream and ObjectInputStream? What...": https://scrivio-interview-library.netlify.app/java/serialization/82d5ca5c30/