Reputation: 16651
In the code base I am working on, nearly all variables that are declared static final String
are also declared transient
.
So I have fields like:
public static final transient String VERSION = "1.0";
I am tempted to remove these transient
keywords whenever I spot them, because I think it doesn't serve any purpose.
Is there any difference in behaviour between using transient
or not in this case?
Upvotes: 9
Views: 6692
Reputation: 21
static members are associated with a class not with the object, hence on deserialization you will see the value you passed on, as opposed to the default values shown while using transient. To have a better understanding try to change the value of the variables after serialization and then on deserialization you will see that the values of serialized members are same but static one has changed. As per the transient final ,final variables do participate in serialization directly by thier values,hence no use of declaring a final variable as transient.
Upvotes: 1
Reputation: 12225
The transient
keyword on a variable will ensure that the variable is not part of the serialized object when serializing. If your class is not serializable
, nor a JPA entity (which uses the transient keyword to avoid storing variables in the database), removing it should be fine.
Upvotes: 2
Reputation: 15275
A static
field is implicitly transient
(when serializing a static
field, its value will be lost anyway). So indeed, no need to declare both.
Upvotes: 12