Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Keywords and Statements

The transient keyword

Reference for the Java transient keyword: exclude fields from default serialization, see what value they get after deserialization, and avoid common mistakes.

transient is a field modifier. It tells Java's built-in serialization (ObjectOutputStream and ObjectInputStream) to skip that field when it turns an object into bytes. When the object is read back, a transient field is not restored from the stream: it holds the default value of its type (0, false, or null).

Use it for data that should not be stored: a cache that can be rebuilt, a password, a reference to a thread or a file handle, or a value that only makes sense inside the running JVM.

Java
class Session implements java.io.Serializable {
    private String user;
    private transient String password; // not written to the stream
}

Try it

Java A transient field after a round trip

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Rebuilding a transient cache

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
[access] transient Type name [= initializer];
Part Description
Where it applies Fields only (instance or static). It cannot be used on local variables, methods, parameters or classes.
Combines with Any access modifier, and final, volatile (see volatile).
Effect Default serialization skips the field. Nothing happens at run time otherwise.

Usage notes

  • Restored value: after deserialization the field is 0, false or null. Field initializers (= 3) and constructors do not run for a Serializable class, so loginAttempts = 3 above comes back as 0.
  • Rebuilding state: if a transient field must be valid after reading, implement private void readObject(ObjectInputStream in) and call in.defaultReadObject() first, or recompute lazily as in the second example.
  • Static fields are never serialized, so marking them transient has no effect.
  • Only default serialization cares. Other libraries decide for themselves what transient means. Jackson skips the field itself but can still pick up a getter, so use @JsonIgnore when you need a guarantee.
  • Records: a record component cannot be transient, because components are always part of the canonical state.
  • Final fields: a transient final field comes back as its default value and cannot be reassigned after construction, so avoid the combination unless the default is what you want.
  • Security: marking a password transient keeps it out of serialized bytes, but it is still in memory. Prefer not storing secrets in serializable objects at all.
  • Java's guidance: native serialization is fragile and a frequent source of vulnerabilities. For new code, prefer JSON or another explicit format.