• 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

what happens when try to serialize static variable

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the question from K&B book

Output would be: 10 0 10

Can anyone explain why s2.z would give 10? What is the result when trying to serialize and deserialize static variable?

Thanks!



[HENRY: Formatted Code]
[ December 11, 2006: Message edited by: Henry Wong ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Output would be: 10 0 10

Can anyone explain why s2.z would give 10? What is the result when trying to serialize and deserialize static variable?



Static variables do not get serialized or deserialized -- the value of s2.z is equal to 10, because it is equal to 10. Deserialization does not change it. If you set s.z to 3 prior to deserialization, you'll notice that s2.z is also 3 after deserialization.

Henry
[ December 11, 2006: Message edited by: Henry Wong ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

note the difference between static and transient variables:
A transient variable is set to the default value (null, 0, 0.0) after deserialization.
Static variables, belonging to the class, are not changed at all.

It is also possible to have a variable that is both, static and transient. It is nonsense, but it compiles. And it behaves as if you made it just static (without the transient modifier).

Yours,
Bu.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic