Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes my static int is serialized, WHY? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "my static int is serialized, WHY?" Watch "my static int is serialized, WHY?" New topic
Author

my static int is serialized, WHY?

adam Lui
Ranch Hand

Joined: Sep 03, 2007
Posts: 186
examine this tiny code...


this may be an illusion, but by the output it looks like the z value of int in specialSerial class is being saved.

WHY? anything i did wrong in this code?


boolean b = true;<br />System.out.println ("I believe in Java.<br />Java will make my dream come " + b);
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
Static variables are not serialized.. In your example, since your are doing the serialization and deserialization using the same JVM instance, every object you create will refer to the same static member, hence changing one static member would change for all objects.
adam Lui
Ranch Hand

Joined: Sep 03, 2007
Posts: 186
thanks
i was expecting a ZERO just like what a transient does
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy, ranchers!

adam Lui posted
i was expecting a ZERO just like what a transient does
No, serialization is for objects only. The static variables belong to the class - not to individual objects - so (de)serialization simply does not touch them at all.

There is no reason to assign any null (or 0) values to the static variables in the serialization process, because the static variables should only be accessed through the class identifier (e.g. SpecialSerial.z=42 in your case).

Yours,
Bu.
[ November 09, 2007: Message edited by: Burkhard Hassel ]

all events occur in real time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: my static int is serialized, WHY?
 
Similar Threads
Serialization
Serialization SCJP KS && BB
Serializing static variables
Why static variable serialized here?
what happens when try to serialize static variable