What is transient variable in java?


Sometime, we need not serialize all the state of the class. Thus for that state or variable we use as transient variable.
The variable declared inside the class with transient keyword are called as transient variable.
Transient variable can’t be serialized. Thus make an instance variable as transient if it should not be saved at time of serialization.
Example:
Import java.net.*;
class Game{
transient String yourid;
String username;
}
Here transient says, don’t save this variable during serialization, skip over it .
Variable username will be saved as part of object’s state during serialization.

1 comment :