wang nan wrote:I don't understand why the last output is "10" ?The static varibles are not serialized when a object is serilized, so I think the resule of "s2.z" must be 0
When the main class is running, there's only 1 variable 'z'.
Its static - so its a class variable. Don't look at s.z and s2.z as instance variables(because they are not).
Replace it with SpecialSerial.z which is actually what it means. So serialization in between has nothing to do with the value of 'z'.
Hence, it is recommended "The static field should be accessed in a static way".
I think we are running this Serialization example in incorrect fashion.
So, reason why change in value of 'z' is getting reflected after deserializing a class is because we are executing serialization and deserialization process from within same main thread.
Since it is same thread , so , when we are changing value of 'z' after serialization , we are changing value of 'z' at class level which is being persisted by JVM because 'SpeicalSerial' class exists in JVM.
Afterwards , we are deserializing it and printing the value of 'z' , JVM takes out the value of 'z' stored at class level and prints it .
I have tried running this example by breaking Serialization and deserialization process in two classes
Class A gives me output as 10
Class B gives me output as 0 9
Normally for running sample code regarding Serialization , we tend to do serialization and de-serialization within same main thread.
But in actual code , written for any application , we use to do serialization and de-serialization in two different classes. after all, that's what serialization concept is all about.
I hope this explains ..........
Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I
Joined: Apr 08, 2009
Thanks for your help. I learn more from your reply. thank you!!