wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 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 "Serialization" Watch "Serialization" New topic


Allen Bandela
Ranch Hand

Joined: Feb 16, 2006
Posts: 128

Hi there!
I wonder what exactly happens to a static variable that has been declared and initialized in a class that implements Serializable and when that class is passed to an output stream with the writeObject() method as follows:

Output: 10 0 10

As static variables would not be serialized, should'nt s2.z return 0? What exactly is happening here. Can someone explain?
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023

Run B.class after runing A.class. You will get s2.z=9. because z is static member and will initialized when the class is loaded (first time).
James Quinton
Ranch Hand

Joined: Oct 02, 2006
Posts: 94
don't get confused by transient.
after being deserialized, transient variable is reset to its default value, while static variable keeps its original value
It is sorta covered in the JavaRanch Style Guide.
subject: Serialization
Similar Threads
Serialization SCJP KS && BB
question about serializable
Why static variable serialized here?
what happens when try to serialize static variable