Quick revision
The short answer
Java serialization writes only an enum constant's name and, on reading, looks it up with valueOf(), ignoring the enum's fields and any custom serialization methods. Jackson also writes name() by default and reads it back by name. So renaming a constant breaks reading old data: Java serialization throws InvalidObjectException and Jackson throws InvalidFormatException. We keep the external value stable through @JsonProperty or @JsonValue, or an explicit mapping, so code can be renamed without breaking stored data, and we avoid ordinal-based options because they are fragile.
What to remember
In this answer, we saw that Java serialization writes only an enum constant's name and resolves it with valueOf() on read, ignoring fields and custom serialization methods, which keeps constants singletons. Jackson also writes name() and reads by name by default. Renaming a constant therefore breaks every stored copy of the old name, with InvalidObjectException or InvalidFormatException. We keep the external value stable with @JsonProperty, @JsonValue or explicit codes, avoid ordinal-based options, and pin the external values with a test.
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 writes only an enum constant's name and, on reading, looks it up with valueOf(), ignoring the enum's fields and any custom serialization methods. Jackson also writes name() by default and reads it back by name. So renaming a constant breaks reading old data: Java serialization throws InvalidObjectException and Jackson throws InvalidFormatException. We keep the external value stable through @JsonProperty or @JsonValue, or an explicit mapping, so code can be renamed without breaking stored data, and we avoid ordinal-based options because they are fragile.
Why this question is asked
Enums end up outside the JVM all the time: in session data, caches, Kafka messages, JSON APIs and database columns. Each of those stores the enum in some external form, and that form outlives the code that wrote it. Interviewers ask how serialization handles enums because a casual rename in an IDE can break production data that nobody thought of. In this answer, we will look at what Java serialization writes for an enum, what Jackson does by default, what exactly breaks on a rename, and how to decouple the external value from the Java name.
1A Quick Recap: Serialization Writes an External Form
Serialization turns an object into bytes or text so it can be stored or sent, and deserialization turns it back into an object. For most classes, Java serialization writes every non-transient field, and deserialization creates a new object and fills the fields in. Enums are treated specially, because creating a new instance would break the guarantee that each constant is a singleton.
2What Java Serialization Writes for an Enum
For an enum constant, ObjectOutputStream writes a marker that says this is an enum, a description of the enum class, and the constant's name as a string. Nothing else. The constant's fields are not written, and methods such as writeObject(), readObject(), readResolve() and writeReplace() are ignored if the enum declares them. The serialVersionUID of every enum is fixed at 0, so it plays no part either.
4 messages in order. Play them, or step through them one at a time.
Only the name crosses the boundary; fields and custom methods play no part.
On reading, ObjectInputStream takes the name and calls Enum.valueOf() for the enum class, so the result is the existing constant. That is why singletons stay singletons and == keeps working after deserialization. It also means a field value in the stream could never be stale, because there is none: the constant's fields come from the current code.
3What Jackson Does by Default
Jackson, the JSON library Spring Boot uses, also uses the name by default. When it writes an enum, it calls name(), not toString(), and when it reads, it matches the JSON string against the constants' names.
Jackson has options that change this. One writes toString() instead of name(), another writes the ordinal as a number, and by default Jackson 2 also accepts a number when reading and treats it as an ordinal. The ordinal options are fragile for the same reason as anywhere else: inserting or reordering constants changes the numbers, so the same JSON would mean a different constant. We should leave them off, and consider enabling the setting that rejects numbers for enums.
4What Breaks When a Constant Is Renamed
Now someone renames PRO to PREMIUM, a harmless refactoring inside the code. Every external form that holds PRO is now a name that no longer exists.
| Store | Typical format | Failure after a rename |
|---|---|---|
| HTTP session store | Java serialization | InvalidObjectException on load |
| Cache (Redis, Hazelcast) | Java serialization or JSON | read errors, cache misses |
| Kafka topic | JSON, Avro | consumer fails on old messages |
| Database JSON column | JSON | InvalidFormatException on read |
| Mobile or web clients | JSON API | requests with the old value rejected |
Notice where the old data lives: HTTP sessions persisted across a restart, a Redis cache with Java-serialized values, messages waiting in a Kafka topic, JSON documents in a database, and clients still on the previous app version. All of them fail after the rename. Some failures are loud, such as a consumer that cannot read its topic. Others are quieter, such as a cache that throws on every read and silently falls back to the database, adding load.
5Keeping the External Value Stable
The fix is to separate the external value from the Java name, so the code can be refactored while stored data keeps its meaning. With Jackson, @JsonProperty on a constant sets the value used in JSON, and @JsonValue on a method makes that method's result the external value for every constant. Reading uses the same value.
For Java serialization there is no such annotation, because the name is the only thing written. If Java-serialized enums must survive a rename, we either keep the old constant name, or avoid Java serialization for durable data altogether, which is wise for other reasons too. For JPA, the same principle means @Enumerated(EnumType.STRING) only while names never change, or an AttributeConverter with an explicit code. And whichever mechanism we use, a test that pins the external value of each constant makes an accidental rename fail the build.
6Follow-up questions
Why are readResolve() and readObject() ignored for enums?
Because the specification makes enum deserialization a lookup by name, so there is no new object to customize. That rule is what guarantees the singleton property; letting enums run custom serialization code would allow it to be broken.
Can Jackson accept both the old and the new name during a migration?
Yes, there are ways: a @JsonCreator factory method that maps both strings, or @JsonAlias on the constant in recent Jackson versions (check your version). That lets us read old data while writing the new value, and later remove the old one.
Does renaming break Java serialization even if serialVersionUID is unchanged?
Yes. Enums ignore serialVersionUID, which is always 0 for them. The only thing that matters is whether the stored name still exists as a constant.
7Conclusion
In this answer, we saw that Java serialization writes only an enum constant's name and resolves it with valueOf() on read, ignoring fields and custom serialization methods, which keeps constants singletons. Jackson also writes name() and reads by name by default. Renaming a constant therefore breaks every stored copy of the old name, with InvalidObjectException or InvalidFormatException. We keep the external value stable with @JsonProperty, @JsonValue or explicit codes, avoid ordinal-based options, and pin the external values with a test.
Open with
For enums, Java serialization writes only the constant's name, plus the class description. It does not write the fields, and it ignores writeObject, readObject, readResolve and serialVersionUID, which is always zero for enums.
Then, if they want more
On reading, ObjectInputStream calls Enum.valueOf with that name and returns the existing constant, which is how enums stay singletons through serialization. Jackson behaves similarly by default: it writes name, not toString, and reads by matching the name. There are options to use toString or the ordinal, but ordinals are fragile because reordering changes them, so I keep them off.
The consequence is that renaming a constant is a breaking change for stored data. If PRO becomes PREMIUM, old Java-serialized bytes fail with InvalidObjectException saying the enum constant does not exist, and old JSON fails with InvalidFormatException. That data can be anywhere: session stores, caches, Kafka topics, JSON columns, older clients.
So I separate the external value from the Java name: @JsonProperty on each constant, or an explicit code field with @JsonValue, and an AttributeConverter for the database. Then the code can be renamed freely. I would also avoid Java serialization for durable data, and add a test that pins each constant's external value.
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 handle enums, and how do JSON libraries such as Jackson...": https://scrivio-interview-library.netlify.app/java/enums/9d92f5b63b/