Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Serialization of static variables  RSS feed

 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a basic question about Serialization:

- During the process of Serailization, the objects state is written to some destination(say ObjectOutputStream) and later read back into memory

- If the object(or any of the contained objects) do NOT implement Serializable interface then an exception is thrown.

- In case the contained object is a static member, then even if it does NOT implement Serializable, then the exception is NOT thrown

- Does this mean that the contained object which is static is NOT written to the OutputStream? If yes, then where is the state of the static object maintained?

Can anyone clarify my doubt
Thank you.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A static variable is part of a class, not part of any one object. Only instance variables are serialized. When you deserialize an object, its class, including any static members, will already have been initialized.
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static members, will already have been initialized


First of all, thank you for the reply. Now, consider a case where there is a static member which has been modified during the execution of a method. After this, the object is Serialized. Now, when the object will be de-serialized, the static memeber will be initialized right? So, what about the modified value?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It'll be gone. You can provide a readObject() method for a class, and that method gets a chance to do additional work during deserialization; it could set the static variable. But if this matters, you may need to rethink your design.
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It'll be gone


This would effectively mean, that for objects that are meant to be Serializable, static variables better be made static final, so that the value wont be changed. Is this understanding right?
 
K Singh
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jaikiran Pai wrote:
It'll be gone


This would effectively mean, that for objects that are meant to be Serializable, static variables better be made static final, so that the value wont be changed. Is this understanding right?


yes this is exactly correct!!
I ll try to explain how it works..

1)When the object is serialized the jvm looks for all the variables which can be serialized , it means jvm will ommit all transient and static variables from the object.
2) After this the JVM goes through the object and by reflection gets all its instance variables.
a) If the object members are not of basic java datatypes(eg Some Class object variables) which doesn't implements Serializable interface it will through an exception (java.io.NotSerializableException)
b) Else JVM will go through all the member variables of the object to be serialized and through reflection it will get all the values of them, and make the links internally.

3) In this step it will generate serialVersionUID by using SHA-1(Secure Hash Algorithm) if the default UID is not given by user.
4) After it is done will give the instance variables to output stream to write in the specified location.

For deserialization following things are done
1) The Serial vesrion uid stored in the object byte stream is matched with the class definition.SHA-1 used again for this
a) If the version id is not matched an exception is thrown
b) else the mapping done during serialization is agian retrived and mapped against the instance variables. During this the static variable is also initialized.
In this step the static variable is intialized . It is not stored in the serialized object.


Please reply if you are not clear.
I tried my best to explain this..

Regards,
Karan Ratra




 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jaikiran Pai wrote:
It'll be gone


This would effectively mean, that for objects that are meant to be Serializable, static variables better be made static final, so that the value wont be changed. Is this understanding right?


Not necessarily. However, if you care about the state the state of a static variable that an object sees after deserialization, then that variable is most likely more properly part of the object, not the class, and should not be static.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!