transient Keyword — Interview Questions
Reviewed by Gurusankar M.
Short Answer
transient marks a field to be skipped during Java serialization. On deserialization that field is NOT restored — it comes back as the type's default (null, 0, false), and any value must be recomputed or re-injected. Use it for derived/cached data, unserializable references (threads, connections), and secrets you must never write to disk or the wire.
On this page
Concept Made Simple
Coffee Chat Question
“What does the transient keyword do, and what are its gotchas?”
Remember It Faster
Mind Map Answer
- Effect
- Field skipped by serialization
- On restore
- Default value (null / 0 / false)
- Use for
- Derived data, non-serializable refs, secrets
- static
- Already not serialized (it's class-level)
The big gotcha: a transient field silently returns as its default after a round-trip. If code assumes it's populated, you get NullPointerExceptions or wrong results only *after* serialize→deserialize — a bug that never shows in unit tests that don't round-trip.
To restore a transient field, implement a custom readObject (Serializable) or readExternal (Externalizable) that recomputes or re-reads it — e.g. reopen a connection, rehydrate a cache, re-derive a checksum.
Key takeaway: transient = 'don't persist this.' Always plan how a transient field gets a valid value again after deserialization.
Learn by Doing
Hands-on Keyboard
class Session implements Serializable {
private String userId;
private transient String authToken; // secret: never serialized
private transient long cachedHash; // derived: recompute on read
private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException {
in.defaultReadObject(); // restores non-transient fields
this.cachedHash = userId.hashCode(); // re-derive the transient value
// authToken stays null — must be re-issued, not restored
}
}Think Beyond the Expected
What If?
Does transient have any effect on a static field?
No — static fields belong to the class, not the instance, so they're never part of an object's serialized state to begin with. Marking a static field transient is redundant (and a code smell that suggests a misunderstanding). transient only matters for instance fields that would otherwise be written out.
Real World
transient keeps passwords, tokens, and open resources (DB connections, sockets, threads) out of serialized session state and caches — both a correctness and a security measure. The classic production bug is a transient cache/derived field coming back null after a session is restored from a distributed store, so teams pair transient fields with a readObject that rehydrates them.
Interviewer's Expectation
Keywords they're listening for:
- skips field on serialization
- restored as default value
- for derived/secret/unserializable fields
- rehydrate via readObject
- static is already excluded
Common Mistakes
- Assuming a transient field keeps its value after a round-trip
- Marking static fields transient (redundant)
- Serializing secrets by forgetting transient
Best Practices
- Mark secrets and open resources transient
- Rehydrate transient fields in readObject/readExternal
- Use transient for derived/cacheable data to shrink payloads
Follow-up Questions
- How do you restore a transient field after deserialization?
- Why mark an authentication token transient?
- How does transient behave with Externalizable?
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Serialization (Advanced Java) Interview question: "What does the transient keyword do, and what are its gotchas?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair