Transient variables in Java - Coddicted

Transient variables in Java

Java’s serialization provides an elegant, and easy to use mechanism for making an object’s state persistent. While controlling object serialization, we might have a particular object data member that we do not want the serialization mechanism to save.
The modifier transient can be applied to field members of a class to turn off serialization on these field members. Every field marked as transient will not be serialized. You use the transient keyword to indicate to the Java virtual machine that the transient variable is not part of the persistent state of an object.
The transient modifier applies to variables only.
Like other variable modifiers in the Java system, you use transient in a class or instance variable declaration like this:
class TransientExample
    transient int hobo;
    . . .
This statement declares an integer variable named hobo that is not part of the persistent state of the TransientExample class.

Can transient variables be declared as ‘final’ or ‘static’?

Java classes often hold some globally relevant value in a static class variable. The static member fields belong to class and not to an individual instance. The concept of serialization is concerned with the object’s current state. Only data associated with a specific instance of a class is serialized, therefore static member fields  are ignored during serialization (they are not serialized automatically), because they do not belong to the serialized instance, but to the class. To serialize data stored in a static variable one must provide class-specific serialization.
Surprisingly, the java compiler does not complaint if you declare a static member field as transient. However, there is no point in declaring a static member field as transient, since transient means: “do not serialize”, and static fields would not be serialized anyway.
On the other hand, an instance member field declared as final could also be transient, but if so, you would face a problem a little bit difficult to solve: As the field is transient, its state would not be serialized, it implies that, when you deserialize the object you would have to initialize the field manually, however, as it is declared final, the compiler would complaint about it.
For instance, maybe you do not want to serialize your class’ logger, then you declared it this way:
private transient final Log log = LogFactory.getLog(EJBRefFactory.class);
Now, when you deserialize the class your logger will be a null object, since it was transient. Then you should initialize the logger manually after serialization or during the serialization process. But you can’t, because logger is a final member as well.There is just one exception to this rule, and it is when the transient final field member is initialized to a constant expression.Hence, field members declared this way would hold their constant value expression even after deserializing the object.

source: XYZWS

Back To Top
Rate this post

Leave a Reply