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 ?
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.
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
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.