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 9294273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:20:00+00:00 2026-06-18T21:20:00+00:00

I have a client/server program, and I’m sending objects through ObjectOutputStream via writeObject() and

  • 0

I have a client/server program, and I’m sending objects through ObjectOutputStream via writeObject() and readObject().

The object I’m sending is class consisting of a couple of fields, and another object inside (let’s call the outer object Wrapper and the inner Inner. Both the custom objects implement serializable.

The first time I send the Wrapper over, everything works flawlessly. Everything stored in both Wrapper, Inner and all the fields serialize and deserialize without problems.

However, when the client then modifies the Inner class, puts it in the Wrapper, and sends it one more time, the Inner instance received by the server, is identical to the one received the first time around.

My client:

Inner inner = new Inner();
inner.setValue("value");

ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
out.writeObject(new Wrapper(inner));

Server:

ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream());
Wrapper wrapper = (Wrapper) in.readObject();
String value = wrapper.getInner().getValue();

Client then modifies the Inner class (same instance as before) with a DIFFERENT string (ie. containing other letters then the first one):

inner.setValue("newValue");
out.writeObject(new Wrapper(inner));

However, when I look at the inner.getValue() on the server, it has not changed and is still equal to "value".

I solved this, by making a hard copy of the inner class, before sending it over:

Inner newInner = new Inner();
newInner.setValue("newValue");
out.writeObject(new Wrapper(newInner));

The new value now is updated as it should.

Why does serialization work this way?

  • 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-06-18T21:20:01+00:00Added an answer on June 18, 2026 at 9:20 pm

    This is the expected behavior of the ObjectOutputStream. To quote from the Javadocs:

    Multiple references to a single object are encoded using a reference sharing mechanism so that graphs of objects can be restored to the same shape as when the original was written.

    Since you are using the same inner class reference, it just sends over a reference to the previously sent object. It does not check all of the fields of all of the objects to see if any of them have changed. I suspect that not only was inner.getValue() equal to the first object sent but that the inner object received by the server in the 2nd object was the same object (==) as the inner from the first object.

    If you had called:

    out.reset();
    

    before the sending of the object with the adjusted inner field, your code should work. The reset() method clears the reference cache which helps the serialization stream be efficient. As a side note, reset() is especially necessary if you are sending a large number of temporary objects across a stream because otherwise these objects are cached in memory and can cause heap exhaustion.

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

Sidebar

Related Questions

I have a client/server program that attempts to send and receive an object. There
I have a server/client program that communicates with serialized objects. When I run the
I have a client-server program. I'm sending data like this: private void Sender(string s,TcpClient
I have a client/server program in TCP written in C, and I would like
all. I'm having a bit of weird problem with client server program. I have
I have a Client program by tcp connection that send data to a server.
I have client-server app. Client on C++, server on Java. I am sending byte-stream
I have a Client-Server program in C#. Here is the Server's code: ... String
In sockets I have written the client server program. First I tried to send
Goal: I have client-server program in which client and server runs in different jvms.

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.