Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

serialization problem

 
Hukm chand
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ....
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Very much for Your help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic