aspose file tools*
The moose likes Java in General and the fly likes Static values don't get serialized, yet the code seems to show the contrary Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Static values don Watch "Static values don New topic
Author

Static values don't get serialized, yet the code seems to show the contrary

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Sierra/Bates, Chapter 6, Question 8




Choices (select all that apply)
1. Compilation fails

2. The output is 10 0 9

3. The output is 10 0 10

4. The output is 10 7 9

5. The output is 10 7 10

6. In order to alter the standard deserialization process you would implement the readObject() method in SpecialSerial

7. In order to alter the standard deserialization process you would implement the defaultReadObject() method in SpecialSerial


According to the book, 3. and 6 are correct. 3 is correct because static and transient variables are not serialized when an object is serialized. 6 is a valid statement.

To me, it seems that static IS serialized because the original value of 10 is restored. On line 22 if I remove static from int z = 9, I get the following output:

10 0 9


Now, z is NOT serialized.

I'm puzzled....

Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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.
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
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
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

   I think you've got it!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static values don't get serialized, yet the code seems to show the contrary