java静态变量序列化 [英] java static variable serialization

查看:41
本文介绍了java静态变量序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在序列化过程中静态变量的值是如何保持的(如果有的话).我在堆栈上读过类似的问题,其中说静态变量本质上是瞬态的,即它们的状态或当前值未序列化.

How are the values of static variables persisted during serialization(If at all persisted). I have read similar questions on stack where it says that static variables are inherently transient i.e their state or current values is not serialized.

我只是做了一个非常简单的例子,我序列化了一个类并将其保存到一个文件中,然后再次从文件中重建了这个类.令人惊讶的是,我发现在序列化发生时静态变量的值被保存了.

I was just doing a very simple example where i serialized a class and saved it to a file and then again reconstructed the class from the file.Surprisingly I find that the value of the static variable at and when the serialization happened is saved.

这是怎么回事.这是因为在序列化期间保存了类模板及其实例信息.这是代码片段 -

How does this happen. Is this because the class template along with it's instance information is saved during serialization. Here is the code snippet -

public class ChildClass implements Serializable, Cloneable{

    /**
     * 
     */
    private static final long serialVersionUID = 5041762167843978459L;

    private static int staticState;
    int state = 0;

    public ChildClass(int state){
        this.state = state;
        staticState = 10001;
    }

    public String toString() {
        return "state" + state + " " + "static state " + staticState;
    }

    public static void setStaticState(int state) {
        staticState = state;
    }

这是我的主课

public class TestRunner {

    /**
     * @param args
     */
    public static void main(String[] args) {
        new TestRunner().run();
    }

    public TestRunner() {

    }

    public void run() {
        ChildClass c = new ChildClass(101);
        ChildClass.setStaticState(999999);
        FileOutputStream fo = null;
        ObjectOutputStream os = null;

        File file = new File("F:\\test");
        try {
            fo = new FileOutputStream(file);
            os = new ObjectOutputStream(fo);
            os.writeObject(c);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {

            try {
                if(null != os)os.close();
                if(null != fo) fo.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }


        FileInputStream fi = null;
        ObjectInputStream ois = null;
        ChildClass streamed;

        try {
            fi = new FileInputStream(file);
            ois = new ObjectInputStream(fi);
            Object o = ois.readObject();
            if(o instanceof ChildClass){
                streamed = (ChildClass)o;
                //ChildClass cloned = streamed.clone();
                System.out.println(streamed.toString());
            }
        } catch (IOException | ClassNotFoundException  e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if(null != ois)ois.close();
                if(null != fi) fi.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }

注意:代码没有任何问题.我只是想知道如何保存类ChildClass"中的静态变量staticState"的值.如果我通过网络传输这个序列化数据,状态会被保存

Note : There is nothing wrong with the code. I just am wondering how the value of the static variable 'staticState' in the class 'ChildClass' gets saved. Will the state be saved if i transmit this serialized data over a network then

推荐答案

静态字段值未序列化.输出正在打印静态字段的新值,因为您将其修改为 999999,但在反序列化之前从未将其值重置为旧值.由于该字段是静态的,新值反映在 ChildClassany 实例中.

The static field value was not serialized. The output is printing the new value of the static field simply because you modified it to 999999 but you never reset its value to the old one before de-serizalizing. Since the field is static, the new value is reflected in any instance of ChildClass.

要正确断言该字段未序列化,请在反序列化对象之前将该值重置为 10001,您会注意到它的值不是 999999.

To properly assert that the field is not serialized, reset the value to 10001 before de-serializing the object, and you will notice that its value is not 999999.

...
ChildClass.setStaticState(10001);

FileInputStream fi = null;
ObjectInputStream ois = null;
ChildClass streamed;

...
// when de-serializing, the below will print "state101 static state 10001"
System.out.println(streamed.toString());

这篇关于java静态变量序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆