Medium👤 3-5 years👤 8-15 years 2 min read

transient Keyword — Interview Questions

Asked inInfosysTCSAccentureOracle
#transient#serialization#java#keywords#security
Report issue

⚡ 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.

Coffee Chat Question

Concept Made Simple

What does the transient keyword do, and what are its gotchas?

🧠Mind Map Answer

Remember It Faster

EffectField skipped by serialization
On restoreDefault value (null / 0 / false)
Use forDerived data, non-serializable refs, secrets
staticAlready 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.

⌨️Hands-on Keyboard

Learn by Doing

java
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
    }
}

🔥What If?

Think Beyond the Expected

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 serializationrestored as default valuefor derived/secret/unserializable fieldsrehydrate via readObjectstatic 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

  • 1How do you restore a transient field after deserialization?
  • 2Why mark an authentication token transient?
  • 3How does transient behave with Externalizable?

🧩Related Technologies

SerializablereadObjectsession storessecurity

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.
Open inChatGPTGeminiClaude

Was this answer helpful?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions