• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

my static int is serialized, WHY?

 
adam Lui
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
i was expecting a ZERO just like what a transient does
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic