Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about serializable

 
wang nan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, you can enclose your source with a code tag to make it more readable. I've edited it for you.
 
wang nan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
wang nan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ..........
 
wang nan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhay Agarwal

Thanks for your help. I learn more from your reply. thank you!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic