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.
Joined: Mar 09, 2005
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?
Joined: Feb 20, 2001
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.