开发者

Serialization of static fields with readResolve()

开发者 https://www.devze.com 2023-02-12 22:44 出处:网络
When I serialize an object I want to also take a \"snapshot\" of the static fields. Is the following code a good practice ?

When I serialize an object I want to also take a "snapshot" of the static fields. Is the following code a good practice ?

public class ClassA implements Serializable {

    private int num1;
    private static int num2 = 5;

    private int num2Saver;

    public int getNum1() {
        return num1;
    }

    public void setNum1(int num1) {
        this.num1 = num1;
    }

    public 开发者_如何学Gostatic int getNum2() {
        return num2;
    }

    public void setNum2(int num2) {
        ClassA.num2 = num2;
        num2Saver = num2;
    }

    private Object readResolve(){
        num2 = num2Saver;
        return this;
    }
}

(notice that in order to set the static field you have to go through an instance )


Maybe you could implement the dedicated private void writeObject(ObjectOutputStream out) throws IOException; method to customize your serialization when you write your objects. Then you have the private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException; when you have to deserialize your objects.

This way you can customize your serialization/deserialization process.

See http://java.sun.com/developer/technicalArticles/Programming/serialization/.


Static member variables don't belong to any particular instance of a class. When you say - ClassA.num2 = num2;, you aren't accessing static variable num2 with any specific instance. You are accessing with class name and that is how static variables need to be accessed though in your case ( because both the argument and static variable bear the same name ).

When accessing with class name, it makes clearer to the reader that it is a static variable. Had if the passing argument bear a different name then, num2 = numX; would have been correct. But this doesn't say to the reader whether num2 is a static variable or a normal class variable.


I don't think it's a good practice. Remember that static members belong globally to the class, and not to a particular instance.

Consider the case of serializing two ClassA instances, with different values of the static member. The serialized form will indeed indicate the two versions of the static data. However, after deserializing the two instances, only one value of the static member will hold (the last one being deserialized).

In other words, you make the value of the static member dependent on the deserialization order.


A static value is not supposed to be serialized. You may wanna reconsider your design if you really need to serialize static value.

Even if you do serialize like the way you have done, that will cause lots of problems for you. The value may be different for each serialized object.

Which value you will use when you deserialize? You can't decide know which one is the correct one.

0

精彩评论

暂无评论...
验证码 换一张
取 消