• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

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

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
   I think you've got it!
 
Something must be done about this. Let's start by reading this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic