What is the effect of static final transient in Java?

In the code base I'm working on, almost all the variables declared by static final String are also declared transient .

So I have fields:

 public static final transient String VERSION = "1.0"; 

I am tempted to remove these transient keywords when I see them, because I think it serves no purpose.

Is there a difference in behavior between using transient or not in this case?

+8
source share
3 answers
Field

A static implicitly transient (when serializing a static field, its value will still be lost). So there really is no need to declare both.

+8
source

The transient keyword for a variable will ensure that the variable will not be part of the serialized object during serialization. If your class is not serializable and not a JPA entity (which uses a transient keyword to avoid storing variables in the database), deleting should be fine.

+2
source

static members are associated with the class, not the object, so when deserializing you will see the value you passed, not the default values โ€‹โ€‹shown when using the transient. To better understand, try changing the value of the variables after serialization, and then during deserialization you will see that the values โ€‹โ€‹of the serialized members are the same, but the static value has changed. According to the transitional final variable , the final variables participate in serialization directly by their values, so there is no need to declare the final variable as a transitional one.

0
source

All Articles