What is Transient Keyword in Java?

What is Transient Keyword in Java?
What is Transient Keyword in Java?
- Transient modifier applicable only for variables but not for methods and classes.
- We can use a transient keyword in serialization context.
- At the time of serialization if we don’t want to save a value of a particular variable to meet security constraint then we should declare that variable as transient.
- At the time of serialization, JVM ignores the original value of transient value variable and save default value to the file. Hence transient means not to serialize.
- Transient keyword plays an important role to meet security constraints. There are various real-life examples where we don’t want to save private data in a file. Another use of transient keyword is not to serialize the variable whose value can be calculated/derived using other serialized objects or system such as the age of a person, current date, etc.
Transient vs static
- The static variable is not part of object state and hence it won’t participate in serialization.
- Due to this declaring static variable as transient, there is no use.
Transient vs final
- The final variable will be participated in serialization directly by the value.
- Hence declaring a final variable as transient there is no impact.