This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!
  • 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 ...
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • Al Hobbs
  • salvin francis

Bug in ObjectOutputStream

Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code demonstrates that ObjectOutputStream may not write the current contents of an object.

We initialize an array to [null] and write it to an output stream. Then we change the value of an array element to a non-null value, and write it again. This program demonstrates that [null] is again written, instead of the current contents of the array.

author and iconoclast
Posts: 24203
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
Not a bug, but documented behavior. ObjectOutputStream keeps track of every object that it writes. When you write an object a second time, a reference to that object is written instead of a new copy of that object's data. This is how circular references are dealt with. So once you write your array, writing it again wwill just cause a reference to the earlier copy to be written; the current state won't be inspected.

You can call reset() on an ObjectOutputStream to flush the cache of old object references and start anew -- but note that if you write an object, change it, call reset(), and write it again, deserialization will give you two separate objects.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic