Static variables cannot be and are not serialized.
Your question seems to be based on the fact that you see the same value from a static variable after serialization as before serialization, but this is not due to the fact that the value is serialized and restored.
This is because the static initializer for this static variable sets it to 9, and it never changes.
To make sure that the static variables are not serialized, you can either perform the NPKR proposed change by changing the static field between serialization and deserialization, or you can do the following:
Run this program, then comment out the bit that performs serialization. As a result, you will get the old serialized version on disk.
Then change the static initializer of the static field to y = 5 and run the program again: you will get "x: 0 y: 5 as the output, because the value 9" of the static field has not been restored.
Theodore murdock
source share