| Author |
question about serializable
|
wang nan
Greenhorn
Joined: Apr 08, 2009
Posts: 8
|
|
the output is 10 0 10
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
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
|
Hi, you can enclose your source with a code tag to make it more readable. I've edited it for you.
|
[My Blog]
All roads lead to JavaRanch
|
 |
wang nan
Greenhorn
Joined: Apr 08, 2009
Posts: 8
|
|
Christophe Verré wrote:Hi, you can enclose your source with a code tag to make it more readable. I've edited it for you.
thank you very much!
|
 |
Kieren Dixon
Greenhorn
Joined: Jan 30, 2009
Posts: 27
|
|
Static and Transient variables are not saved during serialisation.
When you deserialise your object any transient variables will be given a default value. This is why y is set to 0.
|
 |
Sachin Adat
Ranch Hand
Joined: Sep 03, 2007
Posts: 213
|
|
Hello Wang, Welcome to JavaRanch !!!
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".
|
SCJP 6
How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
|
 |
wang nan
Greenhorn
Joined: Apr 08, 2009
Posts: 8
|
|
Kieren Dixon wrote:Static and Transient variables are not saved during serialisation.
When you deserialise your object any transient variables will be given a default value. This is why y is set to 0.
I understand the result of 'y' is 0 . but I don't know why the 's2.z' is 10
|
 |
Abhay Agarwal
Ranch Hand
Joined: Feb 29, 2008
Posts: 697
|
|
Hi
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 certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I
|
 |
wang nan
Greenhorn
Joined: Apr 08, 2009
Posts: 8
|
|
Abhay Agarwal
Thanks for your help. I learn more from your reply. thank you!!
|
 |
 |
|
|
subject: question about serializable
|
|
|