File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what happens when try to serialize static variable

 
Wil Guo
Greenhorn
Posts: 11
  • 0
  • 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
Marshal
Pie
Posts: 20834
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • 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
  • 0
  • 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