• 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

About Object Serialization (Confusion)

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following code segment:

ObjectOutputStream out = new ObjectOutputStream(...);
MyObject obj = new MyObject(); //MyObject class is serializable
obj.setState(100);
out.writeObject(obj);
obj.setState(200);
out.writeObject(obj);

What is the final value of the field "state" in the serialized form?
It's an 100 then why?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks to me like you wrote 2 copies to out, the first with 100, the second with 200.
Bill
 
gunjan bohra
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya offcourse ..and when i got back .I will got the first one means 100
Why it's so ?
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why it's so ?


I don't understand the source of your confusion. An ObjectOutputStream can be used to write one or many Serializable objects.
Bill
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Serialization doesn't write data, it writes objects. What gets written to your stream are two references to the same object. The second reference will not include the object's member data, because the serialization mechanism remembers which objects have already been written out, and by default won't write their data a second time, even if that data has changed.

If an object has already been written, and you want to write a second copy including the values of all the member variables as if the second copy were an altogether separate object, you have to call reset() on the ObjectOutputStream to tell it to forget about any previously-written objects.
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic