From the tutorial SCJP 6 - the question arises about the output of the following code for serialization:
import java.io.*; public class TestClass { static public void main(String[] args) { SpecialSerial s = new SpecialSerial(); try { ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("myFile")); os.writeObject(s); os.close(); System.out.print(++sz + " "); s = null;
Result: 10 0 10
The reason given is that the static variable z is not serialized, and I would not expect this.
The value of the static variable int z increases to 10 after the object has been written to the file, in the expression println ().
In this case, why does this not return to the original value of 9, when the class is deserialized or how the class is not created in the usual way, is the default int value for the class by default, and does not remain with an uninvolved value of 10 after deserialization? I would have thought that a value of 10 would be lost, but it is not.
Has anyone shed light? I stumble here in the dark, wrapping my fingers on this ...
Penelope the duck
source share