David Newton wrote:But... static variables belong to the class definition itself, right?
Change it to something *crazy* after you write it, like 1776. Then see what's printed out after it's read back in... If what you're saying is true, then it should be read in as the same value that was written: but what's actually there is the "crazy" value the variable was changed to after writing. And that's the point of the example--what's "being written" (in your view, it isn't really) is the value 9: *before* the increment. What's "being read" (but isn't really) is 10, which is the original value (what you believe was written) +1.
In a nutshell, the original value is *9*, not 10. It's only 10 after it's incremented, which is happening *after* the write.
I think I understand, especially when you said static variables belong to class definition.
Just to help others, I wrote two sets of code, along with output:
Serialization of a static variable:
Output:
Before serialization
z = 9
After serialization
z = 1776
After deserialization
z = 1776
Serialization of a non-static variable
Before serialization
z = 9
After serialization
z = 1776
After deserialization
z = 9