← Back to Java

transient Keyword

JavaMidjava

The Question

What is the transient keyword in Java?

What a Strong Answer Covers

  • excludes field from serialization
  • transient fields get default values on deserialization
  • used for sensitive/calculated/non-serializable fields
  • static fields also not serialized

Senior-Level Answer

The transient keyword in Java is a field modifier that instructs the serialization mechanism to skip a field when converting an object to a byte stream. When an object implementing Serializable is serialized, every non-transient field is written to the output. A transient field is omitted entirely, and upon deserialization it receives the default value for its type: null for objects, 0 for numbers, false for booleans.

The primary use cases for transient are security and practicality. Sensitive fields like passwords or cryptographic keys should not be persisted to disk or sent over a network in serialized form. Additionally, fields that cannot be serialized — such as a thread, a database connection, or a cached computed value — must be marked transient to prevent a NotSerializableException at runtime.

A classic example is a Logger field. Logger instances are typically singletons retrieved from a factory and are not meaningfully serializable. Marking it transient prevents errors and avoids bloating the serialized form.

After deserialization, transient fields are null (or zero/false). If your class needs to re-initialize them, override the readObject(ObjectInputStream) method to restore them post-deserialization. For example, a transient cached result can be lazily recomputed on first access.

transient has no effect outside Java's built-in serialization. Frameworks like JSON serializers (Jackson, Gson) do not honor it by default — they have their own annotations (@JsonIgnore, @Expose) for field exclusion.

In summary, transient is a targeted escape hatch within the serialization contract: use it to protect sensitive data, avoid serialization errors for inherently non-serializable state, and exclude derived or cached values that are cheaper to recompute than to persist.

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

Correctly states transient excludes a field from serialization and that it receives a default value on deserialization. May not mention security implications or re-initialization via readObject.

3/3 — Strong Answer

Covers exclusion from serialization, default values on deserialization, security use case, non-serializable field use case, and notes transient is ignored by JSON frameworks.

Common Mistakes

  • Assuming transient works with Jackson or Gson — those frameworks use their own exclusion annotations.
  • Forgetting that transient fields are set to default values (null/0/false) after deserialization, not restored automatically.
  • Not mentioning any concrete use case — saying only 'it skips the field' without explaining why you would ever want that.

Follow-Up Questions

  • How do you re-initialize a transient field after deserialization? — Override the private readObject(ObjectInputStream) method to restore or recompute the transient field after default deserialization completes.
  • What happens if a class has a transient field referencing a non-serializable object — is the class still safely serializable? — Yes — marking it transient prevents the NotSerializableException that would occur if the field were included.
  • Does transient affect static fields? — Static fields are not serialized at all regardless of transient, since serialization operates on instance state, not class state.

Related Questions

  • JVM vs JRE vs JDK
  • Java Platform Independence
  • How JVM Works
  • Main Features of Java
  • public static void main

Can You Explain This Cold?

Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.

Get Graded — Free Assessment
GrindQuestionsAITechnical interview assessment
TermsPrivacyAbout