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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:35:01+00:00 2026-05-26T00:35:01+00:00

In the following program. public class SerialExample { public static void main(String args[]) throws

  • 0

In the following program.

public class SerialExample {
    public static void main(String args[]) throws Exception{
        ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("out.txt"));

        Car c = new Car(20);

        os.writeObject(c);

        c.setSpeed(30);

        os.writeObject(c);

        os.close();



        ObjectInputStream in = new ObjectInputStream(new FileInputStream("out.txt"));

        Car d = (Car)in.readObject();
        Car e = (Car)in.readObject();

        System.out.println(d.getSpeed() + "   " + e.getSpeed());

        in.close();
    }
}


class Car implements Serializable{
    private int speed;

    Car(){
        speed = 1;
    }

    Car(int speed){
        this.speed = speed;
    }

    public int getSpeed(){
        return speed;
    }

    public void setSpeed(int speed){
        this.speed = speed;
    }

}

The output of the above program is
20 20.

That means when it is writing the same object the second time instead of writing the object it has written a handler.

My doubt is how does JVM keep track of which all object have already been written to the stream?

  • 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-26T00:35:02+00:00Added an answer on May 26, 2026 at 12:35 am

    The ObjectOutputStream class is responsible for handling this logic.

    From the Java Object Serialization FAQ:

    The ObjectOutputStream class keeps track of each object it serializes
    and sends only the handle if the object is written into the stream a
    subsequent time. This is the way it deals with graphs of objects. The
    corresponding ObjectInputStream keeps track of all of the objects it
    has created and their handles so when the handle is seen again it can
    return the same object. Both output and input streams keep this state
    until they are freed.

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

Sidebar

Related Questions

When I run the following program: public class Test { public static void main(String[]
I came across the following program class Boolean { public static void main(String argv[])
Consider following program: static void Main (string[] args) { int i; uint ui; i
Why does the following program throw an exception? public class MainClass{ public static void
I saw the following program on the internet public class Test1{ public static void
Given the following program: import java.io.*; import java.util.*; public class GCTest { public static
I have the following code: public class Foo {} static class Program { [XmlElement(foo)]
Consider the following program: import java.util.List; import java.util.ArrayList; public class TypeTest { public static
Suppose I've got the following program: namespace ReflectionTest { public class Example { private
I have the following in a program (written in VB.NET): Imports Microsoft.Office.Interop.Excel Public Class

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.