File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static serialization

 
Soni Prasad
Ranch Hand
Posts: 97
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q. Class fields with the following modifiers will not be serialized

1. private
2. static
3. transient
4. protected

ans: 2,3

I could not understand why static fields will not be serialized?

soni.
 
James Carman
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The static fields belong to the class itself. They do not belong to an object. So, when the object is deserialized, the class (along with its static fields) will already be in place. If you did serialize the static members and then deserialized two objects of that class, which value would you use for the static members? The values from the first object or the values from the second object?
 
Soni Prasad
Ranch Hand
Posts: 97
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dont you think static variables value will be same for both the objects as there will be only one copy of the static variable for all the instances of that class. If static variable will not be serialized then on deserialisation static variale be reintialised and it would not be recovered. Is not it a problem with static variables?
 
James Carman
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Imagine this scenario...

1. Serialize object A.
2. Change static variable value.
3. Serialize object B.

Now, if we did serialize the static variables, objects A and B would have different values for the static variable. So, when we deserialize A and B, which value should we use for the static variable?

Again, static variables belong to the class itself, not the individual instances of the class. There will not be one copy of the static variable for all instances of the class. There is only one "copy" of the static variable and it is associated with the class. Each of the instances refer to the same static variable.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic