• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serialization

 
Anit Nair
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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 ]
 
sachin verma
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always use code tag

see this Code Tag
[ May 09, 2008: Message edited by: sachin verma ]
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • 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.
 
Lucy Smith
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
you are right!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic