Why does Java have transient fields?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The transient keyword in Java marks a field to be excluded from serialization. When an object implementing Serializable is written to an ObjectOutputStream, all non-transient fields are serialized. Transient fields are skipped and receive their default value (null, 0, false) upon deserialization. This is essential for fields that contain sensitive data (passwords), derived/cached values, or non-serializable objects (database connections, thread references).
Basic Usage
Why Use transient?
1. Security — Exclude Sensitive Data
Serialized objects may be stored on disk, sent over networks, or logged. Sensitive data in serialized form is a security risk.
2. Non-Serializable Fields
If a field's type does not implement Serializable, the entire object fails to serialize unless the field is marked transient.
3. Derived/Cached Values
Storing derived values wastes space and creates consistency risks. Recalculate them after deserialization.
4. Thread-Related Fields
Restoring Transient Fields After Deserialization
Override readObject to reinitialize transient fields:
transient vs static
Static fields are never serialized because they belong to the class, not the instance. transient is specifically for instance fields you want to exclude.
transient with JSON Libraries
Most JSON libraries (Jackson, Gson) ignore transient fields by default:
transient with JPA/Hibernate
In JPA, transient has a different meaning — use @Transient annotation:
Custom Serialization with transient
Common Pitfalls
- Forgetting to reinitialize transient fields: After deserialization, transient fields are
null(for objects) or0/false(for primitives). Code that assumes these fields are initialized will throwNullPointerException. OverridereadObjectto reinitialize them. - Marking fields transient that should be serialized: Once a field is transient, it is lost during serialization. If you later need the field's value after deserialization, you must remove
transientor implement custom serialization inwriteObject/readObject. - Confusing
transientwithvolatile:transientaffects serialization.volatileaffects thread visibility of a field. They serve completely different purposes and can be combined:private transient volatile boolean running;. - Not setting
serialVersionUID: Without an explicitserialVersionUID, Java generates one based on class structure. Adding or removingtransientchanges the structure, potentially breaking deserialization of previously serialized objects. Always declareserialVersionUID. - Assuming
transientworks with all serialization frameworks: While Jackson and Gson respecttransientby default, some serialization libraries (Kryo, Protocol Buffers) use their own annotation systems. Verify behavior with your specific framework.
Summary
transientexcludes a field from Java serialization — the field becomesnull/default after deserialization- Use it for sensitive data (passwords, tokens), non-serializable objects (connections, threads), and derived/cached values
- Override
readObjectto reinitialize transient fields after deserialization - Static fields are never serialized regardless of
transient - Jackson and Gson respect
transientby default; JPA uses@Transientannotation instead - Always declare
serialVersionUIDto prevent deserialization issues when class structure changes
Related reading
- Why does Java switch on contiguous ints appear to run faster with added cases?
- Why does Java use hash 0x7FFFFFFF tab.length to decide the index of a key?
- Why does Java's Arrays.sort method use two different sorting algorithms for different types?
- Why does Java's hashCode() in String use 31 as a multiplier?
- Why does Java's hashCode in String use 31 as a multiplier?
- Why does java.util.Arrays.sortObject use 2 kinds of sorting algorithms?
- Why does Maven warn me about encoding?
- Why does MockMvc always return empty content?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.