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

Role of static variables in serialization/deserialization  RSS feed

 
Rajiv Rai
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do the static variables in a class have any role
to play when its objects are serialized/deserialized?
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried googling and was surprised by how long it was before I found anything useful. Try this.
 
Rajiv Rai
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Q: What about static variables? Are they serialized?

A: Nope. Remember. static means "one per class" not "one per object".
Static variables are not saved.. and when an object is deserialized ..
it will have whatever static variable its class currently has
.
The moral..don't make serializable objects dependent on a
dynamically-changing static variable! It might be not the same
when the object comes back

 



The above is taken from the Headfirst java book's
chapter on serialization.

What is the meaning of the lines in bold
objects dont have staic variables so when
they are deserialized .. why and how will they
have static variables ?
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why didn’t you say what the problem was yesterday? I could have saved myself the time I spent googling.

Your objects have static fields if the class has static fields. You don’t serialise or de-serialise static fields, so if the static field has changed, the state of the object has changed. So avoid static fields in objects which are serialised and which use those static fields.
 
Rajiv Rai
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell

I apologize if your precious time was lost googling the
query about static fields serialization.

I basically wanted to understand the concept so I asked
a direct question. I could have googled myself but its
better to understand from other learned people here
rather than googling.

After you posted the link .. I thought its better to
quote my source.

Thanks a lot
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:so if the static field has changed, the state of the object has changed.

Hate to disagree, but no. The state of a static field should have nothing whatsoever to do with the state of an object - indeed static variables are generally a bad thing anyway - and since they aren't part of a serialzed object (at least, not unless you force them to be), they can't possibly have anything to with its state upon deserializing.

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajiv Rai wrote:I could have googled myself but its
better to understand from other learned people here
rather than googling.


Actually, it's better to do your own research first, and then if you still have questions, ask them here.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!