This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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 ]
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).