• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

question about serializable

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abhay Agarwal

Thanks for your help. I learn more from your reply. thank you!!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic