All topics
library
intermediate

The transient Keyword

Exclude fields from Java serialization and understand its interaction with default values.

transient tells Java's serialization to skip a field.

transient = a 'do not photograph' sticker on an item in a museum. When someone takes a photo of the exhibit (serialization), that item is left out. When the photo is printed (deserialization), there's a blank space where it was.

Key Concepts

1
    class Session implements Serializable {
      private String userId;
      private transient String authToken;      // not written
      private transient Connection db;         // not Serializable anyway
    }
2
On the way back, a transient field gets its type's default — null, 0 or false. Not the value it had when you wrote it:
null0false
3
    session.authToken;      // null after deserialising
4
Which means code reading it must cope with that. A transient field that something assumes is non-null is a NullPointerException waiting for the first restore.
NullPointerException
5
The four reasons to use it.
    secrets          passwords, tokens, keys
    derived values   a cache you can recompute
    unserialisable   a Connection, Thread, Socket or stream
    scope-local      UI state, progress of a running calculation
6
Without it, a non-serializable field fails the whole write:
    java.io.NotSerializableException: java.sql.Connection
7
The exception names the offending class, which is the quickest way to find what needs marking.
8
static fields are never serialized, with or without transient. They belong to the class, not the instance, so there is nothing per-object to store. Marking a static field transient is harmless and meaningless.
statictransient
9
To restore a transient field, write the hooks:
    private void readObject(ObjectInputStream in)
        throws IOException, ClassNotFoundException {
      in.defaultReadObject();
      this.db = DataSource.get();          // rebuild what was skipped
    }
10
Or encrypt rather than skip:
    private void writeObject(ObjectOutputStream out) throws IOException {
      out.defaultWriteObject();
      out.writeObject(encrypt(authToken));
    }
11
It applies only to Java serialization. Jackson ignores it unless configured otherwise — JSON uses @JsonIgnore, and JPA uses @Transient, which is a different annotation entirely. Three mechanisms, three markers, and mixing them up is a common source of a secret appearing in a log.
@JsonIgnore@Transient
12
What the interviewer is probing.1. "What value does a transient field have after deserialisation?" Probing: the consequence people forget. Stalls: "What it had before." Moves up: the type default — null, zero or false — so any code assuming it is populated throws on the first restore.
13
2. "How do you restore a transient field?" Probing: the hooks. Stalls: "You cannot." *Moves up:* implement readObject, call defaultReadObject, then rebuild the field — reopening a connection, for instance.
14
3. "Does transient affect static fields?" Probing: what is serialised at all. Stalls: "Yes, it excludes them." Moves up: statics are never serialised regardless, because they belong to the class, so the keyword is meaningless there.
15
4. "Does transient work with Jackson or JPA?" Probing: whether you conflate the mechanisms. Stalls: "Yes, it is standard." Moves up: no — JSON uses @JsonIgnore and JPA uses its own @Transient; three mechanisms, three markers, and mixing them up is how a secret reaches a log.