Transient keyword is used along with instance variables to exclude them from serialization process.It provides you some control over serialization process and gives you flexibility to exclude some of object properties from serialization process.
- While performing serialization if we don’t want to serialize the value of a particular variable then we should declare that variable with “transient” keyword.
- At the time of serialization JVM ignores the original value of transient variable and save default value.
- That is transient means “not to serialize”.
Important Points:
- Transient keyword can not be used along with static keyword.Static variable is not part of object state hence they won’t participate in serialization because of this declaring a static variable as transient these is no use.
- Final variables will be participated into serialization directly by their values. Hence declaring a final variable as transient there is no use.
- Transient keyword can only be applied to fields or member variable. Applying it to method or local variable is compilation error.
- Transient variable in java is not persisted or saved when an object gets serialized.