Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why Serialization ignores static fields?

 
rex tony
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why Serialization ignores static fields?
 
Sergey Petunin
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The point of serialization is to save the state of an object, that is, an instance of the class. Static fields do not belong to any instance of the class, they refer to the class itself, so saving and then reloading them for every given object would be no sense anyway.
 
Thirugnanam Saravanan
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After deserialization if i want to retain the value of Static Fields, what should we do?
 
Panseer Kaur
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After deserialization if i want to retain the value of Static Fields, what should we do?


Stick them in instance variables :-)
 
Sergey Petunin
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do nothing. Static fields are absolutely independent of the serialization, because they don't belong to the instance object.
[ December 17, 2007: Message edited by: Serge Petunin ]
 
Gaurav Arora
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thirugnanam Saravanan:
After deserialization if i want to retain the value of Static Fields, what should we do?


You should search for a tutorial on handling serialization requests manually.

In short, you must implement the methods, writeObject and readObject. After writing the objects to the stream, you can simply write your static fields to the stream. While reading, read the object first and then the static variables and initialize them as such, assuming that your static variables can be initialized at runtime. For reference, code snippet.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic