wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what happens when try to serialize static variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what happens when try to serialize static variable " Watch "what happens when try to serialize static variable " New topic
Author

what happens when try to serialize static variable

Wil Guo
Greenhorn

Joined: Jan 09, 2006
Posts: 11
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

Joined: Sep 28, 2004
Posts: 18117
    
  39

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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what happens when try to serialize static variable
 
Similar Threads
Serialization SCJP KS && BB
Why static variable serialized here?
serialization
Serialization
Serializing static variables