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.
class Session implements java.io.Serializable { private String user; private transient String password; // not written to the stream }
Try it
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.
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
[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,falseornull. Field initializers (= 3) and constructors do not run for aSerializableclass, sologinAttempts = 3above comes back as0. - Rebuilding state: if a transient field must be valid after reading, implement
private void readObject(ObjectInputStream in)and callin.defaultReadObject()first, or recompute lazily as in the second example. - Static fields are never serialized, so marking them
transienthas no effect. - Only default serialization cares. Other libraries decide for themselves what
transientmeans. Jackson skips the field itself but can still pick up a getter, so use@JsonIgnorewhen you need a guarantee. - Records: a record component cannot be
transient, because components are always part of the canonical state. - Final fields: a
transient finalfield 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
transientkeeps 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.
Related
final,static,volatile,class- Lessons: Serialization, Fields and methods