Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7513655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:15:03+00:00 2026-05-30T00:15:03+00:00

I have a serialized object MyObject that contains integer foo. I set a value

  • 0

I have a serialized object MyObject that contains integer foo. I set a value 10 to integer foo and save the object to a file using writeObject().

I add integer bar to object MyObject. I set a value 15 to integer bar and then load the old serialized file using readObject().

The old serializable file doesn’t contain integer bar so integer bar will get value 0. I want to keep the value 15 in bar if the old serializable file doesn’t contain variable bar.

Should I override readObject() or how could I prevent readObject() from setting “default values” to unknown objects?

I want to do this because in the constructor I’m setting my own default values and would like to use my own default values to control versioning.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:15:06+00:00Added an answer on May 30, 2026 at 12:15 am

    Serialization doesn’t set default values it defers to Java’s default value initialization scheme.

    If I can sum up your question. You want serialization to merge what’s in the serialized stream with the values in memory. That’s not possible with Java serialization as it controls what objects to create. You can read in your serialized object, then manually write the code to merge what fields you want merged together. If your stuck on Java serialization (I’d steer clear of it if I were you), but let’s say you want to continue using it.

    public class MyObject {
    
        public void merge( MyObject that ) {
            // given some other instance of an object merge this with that.
            // write your code here, and you can figure out the rules for which values win.
        }
    }
    
    ObjectInputStream stream = new ObjectInputStream( new FileInputStream( file ) );
    MyObject that = stream.readObject();
    someObject.merge( that );
    

    Viola you control which fields will be merged from that into someObject. If you want a library to do this merge for you check out http://flexjson.sourceforge.net. It uses JSON serialization, and works from Beans rather than POJO. However, there is a way to take an already populated object and overwrite values from a JSON stream. There are limitations to this. The other benefit of this is you can actually read the stream back AFTER your object structure has changed something that Java serialization can technically do, but its very very hard.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pretty sizeable object graph that I have serialized to a file
I have an object that is mapped to a cookie as a serialized base-64
I have an XML file that I'm trying to serialize into an object. Some
I have a class that serializes a set of objects (using XML serialization) that
I have an object in Javascript that looks like this function MyObject(){ this.name=; this.id=0;
I have an object graph that contains objects that are (for purposes of this
If I have 10 different objects serialized/deserialized using a Generic T class that uses
I have an object graph that I need to serialize to xml and save
I have an object model called MyObject that I'm serializing to a json string
I am using C# 2.0 with Nunit Test. I have some object that needs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.