-2

I'm going through serialization and I can't understand the following:

I don't understand why the output of this code is:

import java.io.*;

public class InnerOuterTest {
    public static ObjectOutputStream out;
    public static ObjectInputStream in;

    static {
        try {
            out = new ObjectOutputStream(new FileOutputStream("save.ser"));
            in = new ObjectInputStream(new FileInputStream("save.ser"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        try {
            ShouldForgive f = new ShouldForgive();
            f.x = 5;
            write(f);
            ShouldForgive g = read();
            System.out.println(g.x);
            f.x = 0;
            g.x=8;
            write(f);
            ShouldForgive v = read();
            System.out.println("is "+v.x);
        } finally {
            out.close();
            in.close();
        }
    }

    private static void write(ShouldForgive f) throws IOException {
        out.writeObject(f);
    }

    public static ShouldForgive read() throws ClassNotFoundException, IOException {
        return (ShouldForgive) in.readObject();
    }
}

class ShouldForgive implements Serializable {
    int x = -1;
}

Is

5
8

And not

5
0

I tried f == g which returns false and if I reset the input stream. I found out that if I implement readObject it is only called once... I don't understand this behavior. (Why is the object only read once?)

I get the feeling that serialization only happens once... How is the object tracked? Even if I implement readObject and writeObject without actually reading or writing from the file I still get 8

2 Answers 2

2

If you call out.reset() after the first write, you will get the behaviour you are expecting, or if you use writeUnshared() instead of writeObject(). Objects that have already been written to the stream are not rewritten; instead a 'handle' to the previously written object is written. See the Javadoc for details.

Sign up to request clarification or add additional context in comments.

Comments

-1

OK...I think i got it...

  1. During serialization, its NOT the object which is serialized, but its Instance variable... as extracted and stored. For an object its instance variable is everything...

  2. So during de-serialization, the same instance variables are inflated back, and using those instance variable value, an Another Identical object is created on the heap..

  3. If you check the hashcode of both these objects, they will be different.

Thats causing the above behaviour mentioned by you.

Either follow this sequence

f.x = 0;
g.x=8;

or this

g.x=8;       
f.x = 0; 

Still the result will be 8, cause its now a completely different object on the heap with different instance variables.

Edited part

The below code will print f as 5..... for both the sysout

public static void main(String[] args) throws IOException, ClassNotFoundException {
        try {
            ShouldForgive f = new ShouldForgive();
            f.x = 5;
            write(f);
            ShouldForgive g = read();
            System.out.println(g.x);

            f.x=0;
            write(f);
            ShouldForgive v = read();
            System.out.println("is "+v.x);
        } finally {
            out.close();
            in.close();
        }
    }

3 Comments

Not quiet clear. If they are two different objects, how write "f" writing value "8"?
The hashcodes are quite likely to be the same if the object is well written.
Not the correct answer. And the hashcodes can still be the same if the objects are different instances.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.