aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes serialization problem 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 problem" Watch "serialization problem" New topic
Author

serialization problem

Hukm chand
Greenhorn

Joined: Feb 20, 2009
Posts: 16
hai,
I studied serialization in K&B book.I wrote a code to check my understanding.. i learn serialization is used to current object state.so i try to change the state of object and store it.

output I Got here..
value of d's number before: is 10
value of d2's number before: is 50
value of d1 before is1000
value of d's number after is10//here right
value of d1's numbe rafter is50//here it have to show 1000
value of d2's number after is10// here it have to show 50

help me what's wrong in this code ....


preparing for SCJP 6
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

help me what's wrong in this code ....


First, a bit of side information.... The serialization code actually keeps track of what has been written. And if you write an object twice, it will simply mark it as a repeated write -- and not actually write it.

Why is this needed? Let's say you have a circular linked list, and you write one of the nodes. If this behavior didn't exist, the serialization code will contiue traversing the linked list, forever writing the nodes that it finds. With this behavior, eventually, it will just write that it encountered an object that was already written and stop.

On the deserialization side, it will get a message that the object was a read of an eariler object, and get the same reference as the earlier dersialized object.

Now back to your question...



When you did "d1=d", you actually assigned the d1 variable to the same object as the d variable -- an object that already have been serialized.

So, when you later write d1, it actually doesn't do it -- it just writes that it has been written eariler (and marked to the location with the previous values). When you deserialize, you will get the same reference for the first and third object -- and it deserializes to the values that was written first.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


BTW, anticipating your next question ... Take a look at the java.io.ObjectOutputStream class. Specifically, take a look at the reset() method.

Henry
Hukm chand
Greenhorn

Joined: Feb 20, 2009
Posts: 16
Thank You Very much for Your help
 
wood burning stoves
 
subject: serialization problem