• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

my static int is serialized, WHY?

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
i was expecting a ZERO just like what a transient does
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Do not set lab on fire. Or this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic