🚀 Enjoying Java Tutorials?
Subscribe to Ram N Java for clean, beginner-friendly core Java tutorials and interview prep!
🔔 Subscribe to Ram N JavaIntroduction
When serializing objects in Java, not every field should be saved to persistent storage or sent over the network. Sensitive details like passwords, encryption keys, or temporary session identifiers need to be omitted. Java provides a dedicated keyword for this exact purpose: transient.
What Does the Transient Keyword Do?
The transient modifier is a variable-level modifier used during serialization:
- When an object is serialized using
ObjectOutputStream, any instance variable marked astransientis completely ignored by the default JVM serialization mechanism. - Its value is not written to the byte stream.
What Happens During Deserialization?
Because the transient variable's value was never persisted into the byte stream, Java assigns it the default value of its data type when the object is restored via ObjectInputStream:
- For reference types / objects (e.g.,
String, custom classes):null - For numeric primitive types (e.g.,
int,short,long):0 - For floating-point primitives (e.g.,
float,double):0.0 - For boolean primitives:
false
Transient with Static and Final Variables
| Modifier Combination | Serialization Behavior |
|---|---|
transient instance variable |
Skipped; restored to default value (e.g., null, 0) |
transient static |
Static fields belong to the class, not object state; transient has no extra effect |
transient final |
Direct value gets serialized as a constant compile-time value |
Related Tutorials from Ram N Java
Click here to watch in Youtube :
https://www.youtube.com/watch?v=EtIhgC2nb_I&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge
![]() |
| Java Tutorial : Java IO (Java Serialization with Transient keyword) |


No comments:
Post a Comment