Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Serializatin & Clonning  RSS feed

 
Deepak Nambiar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have one class say Employee which has a reference of another class Company. I created one instance of Employee --> e1 . set all the values and created a clone from e1 using shallow copying thus creating e2. I serialized e1 and used cloned one e2 to change some variable values of Company instance. Since both e1 and e2 have the same reference of Company I believe this must alter e1's Company variables values also. Then I deserialized the e1 to e1_deserial and tried to display the Company's variable. But its not showing the changed one. while serializing only reference will be saved or value also will be saved.? Please throw some light in this topic
 
Sunny Bhandari
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
References are not serialized but objects are serialized.

What is the fun of storing your name if it can't point to you/your body/your mind when I restart this universe...

Hope you got that...
 
Greg Charles
Sheriff
Posts: 3014
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you deserialized e1, it created a new instance of both the employee class and the company class. In some ways the serialization is pretty smart. In your example you have e1 and e2, which both hold references to the same company instance ... let's call it c1. If you put both e1 and e2 into a collection and serialized the collection, not only would the serialization realize that it should only store c1 once, but when you deserialized the collection, I'm pretty sure both the new e1 and e2 would again share a reference to the new c1. However, if you serialized e1 and e2 in separate calls, or in your case serialized one of them then immediately deserialized it, you'll end up with two different Company instances.

 
Deepak Nambiar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question was while serializing a object holding reference of another object , what happens to the reference . is reference is saved while serializing the object with its values or not
 
Greg Charles
Sheriff
Posts: 3014
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the objects and their relationships will be serialized and restored on deserialization, but not the original references. Generally speaking, you won't be deserializing objects into an environment where their original references still exist anyway. For example, you serialize an object from a client program to a server. The server never had the original references. Or you save a serialized object to a file, then a few days later read it back. Even if you haven't shut down the JVM in all that time, it's likely that the original references are long gone. That's what serialization is meant for, so it's not going to save anything that's relevant only to the particular runtime environment you serialize from. Hope that helps!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!