• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Serialization

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per K&B serialization is not for static variables....but i tried out following piece of code and it shows that static variables are also serialized and saved as part of object state..I m a bit confused now. Please find below the code i executed


Also, if i add static to transient member variables the member variables are still serialized..shouldnt adding transient member variables be skipped
while serialization???
[ May 09, 2008: Message edited by: Anit Nair ]
[ May 09, 2008: Message edited by: Anit Nair ]
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please always use code tag

see this Code Tag
[ May 09, 2008: Message edited by: sachin verma ]
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anit, if you run your program you can see that the object you are serializing is obj and its instance value is 6 but when you de-serialize the object the value of i is changed to 7. So in effect serialization failed for the obj instance. It didn't save the obj's instance value.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sridhar,

That's not correct. Since those variables are defined as static, it doesn't matter what the instance is. After de-serialization the name being obj is just shadowing.

I think Anit has a point and will wait for some expert's take on this.
 
Anit Nair
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sridhar, thats exactly what i want to say....static member are not part of objects state....then why is the value for it stored in the first place...
If you refer the K&B pg 456 under topic [Serialization not for statics]
The last line clearly states "Static variables are NEVER SAVED as part of object state...because they do not belong to the object". Then why is 7 saved in this case...
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Since those variables are defined as static, it doesn't matter what the instance is



Yes thats the whole point, Serialization is saving an objects state and not a class's state. When you have static variables they are part of the class and not any particular instance. Serialization applies only to objects.

After de-serialization the name being obj is just shadowing.



The var name obj is the same name but it is not shadowing. The object is deserialized and casted to DemoSerial.

The last line clearly states "Static variables are NEVER SAVED as part of object state...because they do not belong to the object". Then why is 7 saved in this case



Yes Static variables are NEVER SAVED as part of object state. 7 is saved because you changed the static variable value using an instance of another object (obj2) and you are trying to save the object referred by obj.

Hope this below code helps



but if you change int i from static to non-static


[ May 09, 2008: Message edited by: sridhar row ]
 
Anit Nair
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sridhar, I guess i have understood....whats happening in this case. Actually, the value is never getting stored at all, i guess. Its just that when you de-serialize and cast the object to (DemoSerial), when you execute the following line


Its similar to calling DemoSerial.i hence the value 7. It just prints the current value of i. not the value belonging to any instance of DemoSerial. Thanks for your inputs
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are right!!
 
If you are using a wood chipper, you are doing it wrong. Even on this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic