Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does deserialized object preserve static values?

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.

like image 322
Vic Avatar asked Apr 26 '16 08:04

Vic


People also ask

What happens to static variables during serialization?

In Java, serialization is a concept using which we can write the state of an object into a byte stream so that we can transfer it over the network (using technologies like JPA and RMI). But, static variables belong to class therefore, you cannot serialize static variables in Java.

Can we save static data member using serialization?

Static data members and transient data members are not saved via Serialization process.So, if you don't want to save value of a non-static data member then make it transient. 4. Constructor of object is never called when an object is deserialized.

What is Deserialized object?

Deserialization is the process of reconstructing a data structure or object from a series of bytes or a string in order to instantiate the object for consumption. This is the reverse process of serialization, i.e., converting a data structure or object into a series of bytes for storage or transmission across devices.

Which items should never be serialized Deserialized?

Examples of sensitive data that should never be serialized include cryptographic keys, digital certificates, and classes that may hold references to sensitive data at the time of serialization.


1 Answers

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().

like image 73
Andreas Fester Avatar answered Oct 10 '22 08:10

Andreas Fester