为什么Java有瞬时字段?


当前回答

简单地说,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;

   }
}

其他回答

因为并非所有变量都具有可序列化的性质

根据谷歌短暂的意义==只持续很短的时间;短暂的

现在,如果您想在java中实现任何瞬态,请使用瞬态关键字。

Q: 在哪里使用瞬态?

A: 通常在java中,我们可以通过在变量中获取数据并将这些变量写入文件来将数据保存到文件中,这个过程称为序列化。现在,如果我们想避免将变量数据写入文件,我们会将该变量设置为瞬时变量。

transient int result=10;

注意:瞬态变量不能是局部变量。

简单地说,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;

   }
}

允许您定义不希望序列化的变量。

在对象中,您可能有不希望序列化/持久化的信息(可能是对父工厂对象的引用),或者序列化没有意义。将这些标记为“瞬时”意味着序列化机制将忽略这些字段。

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

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);
    }
}