为什么Java有瞬时字段?


当前回答

在我回答这个问题之前,我需要解释序列化,因为如果你理解了它在科学计算机中的含义,你可以很容易地理解这个关键词。

当对象通过网络传输/保存在物理介质(文件,…)上时,该对象必须“序列化”。序列化转换字节状态对象序列。这些字节在网络上发送/保存,并根据这些字节重新创建对象。

例子:

public class Foo implements Serializable 
{
 private String attr1;
 private String attr2;
 ...
}

现在,如果这个类中有一个字段不想传输或保存,可以使用transient关键字

private transient attr2;

这防止在序列化类时包含字段表单。

其他回答

当您不想共享一些与序列化相关的敏感数据时,需要使用它。

瞬态变量是在类序列化时不包含的变量。

我想到的一个可能有用的例子是,只有在特定对象实例的上下文中才有意义的变量,并且在序列化和反序列化对象之后,这些变量就会变得无效。在这种情况下,让这些变量变为空是很有用的,这样您就可以在需要时用有用的数据重新初始化它们。

本机java系统以外的序列化系统也可以使用此修饰符。例如,Hibernate不会持久化标记为@Transient或Transient修饰符的字段。Terracotta也尊重这个修饰语。

我认为修饰语的比喻意义是“此字段仅供内存使用。不要以任何方式将其持久化或移出此特定VM。它不可移植”。也就是说,你不能依赖它在另一个VM内存空间中的值。与volatile非常类似,意味着您不能依赖特定的内存和线程语义。

瞬时关键字的简化示例代码。

import java.io.*;

class NameStore implements Serializable {
    private String firstName, lastName;
    private transient String fullName;

    public NameStore (String fName, String lName){
        this.firstName = fName;
        this.lastName = lName;
        buildFullName();
    }

    private void buildFullName() {
        // assume building fullName is compuational/memory intensive!
        this.fullName = this.firstName + " " + this.lastName;
    }

    public String toString(){
        return "First Name : " + this.firstName
            + "\nLast Name : " + this.lastName
            + "\nFull Name : " + this.fullName;
    }

    private void readObject(ObjectInputStream inputStream)
            throws IOException, ClassNotFoundException
    {
        inputStream.defaultReadObject();
        buildFullName();
    }
}

public class TransientExample{
    public static void main(String args[]) throws Exception {
        ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("ns"));
        o.writeObject(new NameStore("Steve", "Jobs"));
        o.close();

        ObjectInputStream in = new ObjectInputStream(new FileInputStream("ns"));
        NameStore ns = (NameStore)in.readObject();
        System.out.println(ns);
    }
}

简单地说,transient java关键字保护字段不被序列化为其非transient字段计数器部分。

在这个代码片段中,我们的抽象类BaseJob实现了Serializable接口,我们从BaseJob扩展,但不需要序列化远程和本地数据源;仅序列化organizationName和isSynced字段。

public abstract class BaseJob implements Serializable{
   public void ShouldRetryRun(){}
}

public class SyncOrganizationJob extends BaseJob {

   public String organizationName;
   public Boolean isSynced

   @Inject transient RemoteDataSource remoteDataSource;
   @Inject transient LocalDaoSource localDataSource;

   public SyncOrganizationJob(String organizationName) {
     super(new 
         Params(BACKGROUND).groupBy(GROUP).requireNetwork().persist());

      this.organizationName = organizationName;
      this.isSynced=isSynced;

   }
}