I just finished Chapter 6 of Sierra/Bates, and now I am on Chapter 7 that begins its discussion with Hashing. I modified the following code, based on Chapter 7:
My output is:
45 50
0 50
I would think that the output is:
45 50
0 5
because in deserialization, the constructor is called, and y gets it's static value of 5. Please explain.
The constructor is not called for your 'Serializable' class. If the JVM must reload
the class, however, the statics will revert to their initialized values. Otherwise, if
you serialize and then immediately deserialize, static values are untouched.
When recreating an instance from a serialized object, the constructor with no parameters is used. Because you did not specify this constructor, the compiler creates one for you.
EDIT: I tried my own test (creating a constructor with no parameters) and found I was wrong. Looks like it's back to reading the book for me :-)
Where can I find an example program that demonstrates static variables reverting to their original values after class is de-serialized. Or better yet, how can I modify the code I posted to perform this action. Please guide.