aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Serialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Serialization" Watch "Serialization" New topic
Author

Serialization

Anit Nair
Greenhorn

Joined: May 09, 2008
Posts: 12
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

Joined: Mar 25, 2008
Posts: 177
Please always use code tag

see this Code Tag
[ May 09, 2008: Message edited by: sachin verma ]

SCJP || SCWCD

sridhar row
Ranch Hand

Joined: Jan 16, 2008
Posts: 162
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

Joined: Aug 07, 2007
Posts: 17
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

Joined: May 09, 2008
Posts: 12
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

Joined: Jan 16, 2008
Posts: 162
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

Joined: May 09, 2008
Posts: 12
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

Joined: Jan 16, 2008
Posts: 162
you are right!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serialization