6

Here's a very basic test program:

public class Body implements Serializable {
    static int bod = 5;
    int dis = -1;
    public void show(){
        System.out.println("Result: " + bod + " & "  + dis);
    }
}

public class Testing {
    public static void main(String[] args) {
        Body theBody = new Body();
        theBody.show();
        try {
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("test.dat"));
            out.writeObject(theBody);
            out.close();
            ObjectInputStream in = new ObjectInputStream(new FileInputStream("test.dat"));
            Body bodyDouble = (Body)in.readObject();
            in.close();
            bodyDouble.show();
        } catch(IOException e) { 
        } catch(ClassNotFoundException e) {
        }

    }
}

But I don't understand why its output is:

Result: 5 & -1
Result: 5 & -1

Since static members are not serialized and hence get default values I was expecting another output:

Result: 5 & -1
Result: 0 & -1

How did the deserialized object acquire the right static field value?

I've made this test application because I need to serialize a few objects with a number of static fields in order to make deep copies (and of course, I need to be sure that copies have the same static fields' values since they are used as array indices).

And now I'm absolutely confused about what happens after deserialization. On the one hand, static members don't get serialized but, on the other hand, as the example prooves, static members somehow preserve their values. I suspect that it has something to do with casting of readObject() to the Body object but I'm not sure.

Vic
  • 211
  • 2
  • 9
  • http://stackoverflow.com/questions/1008023/how-to-serialize-static-data-members-of-a-java-class – rkosegi Apr 26 '16 at 08:34

1 Answers1

6

How did the deserialized object acquire the right static field value?

Because the static field values have not changed between serialization and deserialization:

You are running your test within the same application. Hence the static class members retain their value. The class iteself is not reloaded or re-initialized - it is still the same.

You should separate your test in two separate applications:

  • Let one application serialize the object
  • Let one application deserialize the object

Then run these two applications one after the other. You will then see that the static members are not restored in your second application.

Alternatively, in your code above, you could also set the static field to a different value right before deserialization - you will then see that the variable still has this value after deserialization.

On a side note, never do ... catch(IOException e) {} ... - at least, print the stack trace with e.printStackTrace().

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • Thank you. So do I get it right that since I need a temporary identical deep copy of an object at runtime (with no separate application involved) I don't need to worry about static fields - all deserialized copies will have them? I.e. losing static values is basically a problem of object exchanges between different applications? – Vic Apr 26 '16 at 12:50
  • Yes. Because serialization is about objects (class instances), not classes. This might be useful for your specific use case: http://stackoverflow.com/questions/64036/how-do-you-make-a-deep-copy-of-an-object-in-java – Andreas Fester Apr 26 '16 at 13:16