• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

serialization problem

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ....
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Very much for Your help
 
reply
    Bookmark Topic Watch Topic
  • New Topic