Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

what happens when try to serialize static variable  RSS feed

 
Wil Guo
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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 ]
 
Henry Wong
author
Sheriff
Posts: 22818
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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 ]
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • 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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!