transient keyword

Transient keyword in Java

The transient keyword is used in serialization. If you define any data member as transient, it will not be serialized. Let’s take an example, I have declared a class as Student, it has three data members id, name and age. If you serialize the object, all the values will be serialized but I don’t want to serialize one value, e.g. age then we can declare the age datamember as transient.

Example of transient keyword

In this example, we have created the two classes Student and Persist. One data member of the Student class is declared as transient, it value will not be serialized. If you deserialize the object, it will return the default value for transient variable.

 


 

Facebook