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
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.