序列化Java中的最终瞬态如何工作 [英] How transient works with final in Serialization Java

查看:314
本文介绍了序列化Java中的最终瞬态如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关transient和final关键字的内容,我找到了一个答案,我们不能使用带有final关键字的transient关键字。我试过并感到困惑,因为它工作正常。

I was reading about transient and final keyword and I found the answer that we can't use transient keyword with final keyword. I tried and got confused because here it working fine.

import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.Serializable;

public class SerExample{
    public static void main(String... args){
        Student foo = new Student(3,2,"ABC");
        Student koo = new Student(6,4,"DEF");
        try
        {
            FileOutputStream fos = new FileOutputStream("abc.txt");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(foo);
            oos.writeObject(koo);
            oos.close();
            fos.close();
        }
        catch(Exception e){/**/}

        try{
            FileInputStream fis = new FileInputStream("abc.txt");
            ObjectInputStream ois = new ObjectInputStream(fis);
            System.out.println(ois.readObject());
            System.out.println(ois.readObject());
            fis.close();
            ois.close();
        }catch(Exception e){/**/}
    }
}

这是Serializable Student类代码:

Here is the Serializable Student class Code:

class Student implements Serializable{
        private transient final int id;
        private transient static int marks;
        private String name;
        public Student(int id, int marks, String name){
            this.id = id;
            this.marks = marks;
            this.name = name;
        }
        public Student(){
            id=0;
        }
        @Override
        public String toString(){
            return (this.name + this.id + this.marks);
        }
    }

带有transient关键字的代码输出。

Code output with transient keyword.

ABC04
DEF04

没有临时关键字的输出。

Output without transient keyword.

ABC34
DEF64

你能解释为什么它工作正常吗?有没有错误?

Can you explain why it's working fine? is there a bug?

最后什么应该是带有final关键字的瞬态行为?

At the end what should be behavior of transient with final keyword?

推荐答案

你的问题有点重复:

  • final-transient-fields-and-serialization
  • a-transient-final-field-used-as-a-lock-is-null

最终字段必须通过直接赋值初始值或在构造函数中初始化。在反序列化期间,这些都不会被调用,因此必须在反序列化期间调用的'readObject()'私有方法中设置瞬态的初始值。为了工作,瞬态必须是非最终的。

A final field must be initialized either by direct assignment of an initial value or in the constructor. During deserialization, neither of these are invoked, so initial values for transients must be set in the 'readObject()' private method that's invoked during deserialization. And for that to work, the transients must be non-final.


任何声明为transient的字段都不是序列化的。此外,根据此博客文章,字段值甚至不会初始化为默认构造函数设置的值。当瞬态场是最终的时,这会产生挑战。

Any field that is declared transient is not serialized. Moreover, according to this blog post, field values are not even initialized to the values that would be set by a default constructor. This creates a challenge when a transient field is final.



至于测试结果:



As for the results of your test:


带有transient关键字的代码输出。
ABC04 DEF04

没有transient关键字的输出。
ABC34 DEF64

Code output with transient keyword. ABC04 DEF04
Output without transient keyword. ABC34 DEF64



瞬态



显然, transient 字段(第4个字符)未被序列化/反序列化(ABC 3 4-> ABC 0 4且DEF 6 4-> DEF 0 4)

transient

Clearly, the transient field (4th character) is not being serialized/deserialized (ABC34->ABC04 and DEF64->DEF04)

static 字段(第5个字符)也没有被反序列化!这只是因为您在同一个内存空间中执行操作,并且静态字段保留在所有实例中。因此,当您在student上设置静态字段,然后反序列化另一个学生时,静态字段当然仍然具有相同的值!

The static field (5th char) is also not being deserialized! It's simply because your are performing the operation in the same memory space, and the static field remains across all instances. So when you set the static field on student, and later deserialize another student, of course the static field still has the same value!

这也解释了为什么在你的测试中你首先将静态字段设置为 2
然后 4 ,但只打印 4 。在这种情况下,与序列化无关,只是静态字段行为。

This also explain why in your test you first set the static field to 2 and then 4, but only 4 gets printed. Nothing to do with serialization in this case, simply static field behavior.

这篇关于序列化Java中的最终瞬态如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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