aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static serialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static serialization" Watch "static serialization" New topic
Author

static serialization

Soni Prasad
Ranch Hand

Joined: Mar 09, 2005
Posts: 97
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.


SCJP 1.4, SCBCD 1.3
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
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?


James Carman, President<br />Carman Consulting, Inc.
Soni Prasad
Ranch Hand

Joined: Mar 09, 2005
Posts: 97
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

Joined: Feb 20, 2001
Posts: 580
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
 
subject: static serialization