File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
I agree. Here's the link:
subject: Serialization
It's not a secret anymore!