Transient keyword in Java

Transient is the modifier applicable only for variables.

While performing serialization if we don't want to save the value of a particular variable to meet security constant such type of variable, then we should declare that variable with the "transient" keyword. At the time of serialization, JVM ignores the original value of the transient variable and saves the default value to the file.

That is transient means "not to serialize".

According to Java Language Specification [jls-8.3.1.3] – "Variables may be marked transient to indicate that they are not part of the persistent state of an object."

Syntax:

private transient member_variable;

or
transient private member_variable;

Static Vs Transient :

A static variable is not part of the object state hence they won't participate in serialization because of this declaring a static variable as transient there is no use.

Transient Vs Final:

final variables will be participated into serialization directly by their values. Hence declaring a final variable as transient there is no use. the compiler assigns the value to the final variable with default values.

In case you define any data member as transient, it will not be serialized. This is because every field marked as transient will not be serialized. You can use this transient keyword to indicate the Java virtual machine (JVM) that the transient variable is not part of the persistent state of an object.

In this article, we have seen the Transient keyword in Java with Examples.