Why does Java have transient fields?

前端 未结 15 1969
旧时难觅i
旧时难觅i 2020-11-22 03:54

Why does Java have transient fields?

15条回答
  •  悲哀的现实
    2020-11-22 04:31

    Simply put, the transient java keyword protect fields from the been Serialize as their non-transient fields counter parts.

    In this code snippet our abstract class BaseJob implement Serializable interface, we extends from BaseJob but we need not serialize the remote and local data sources; serialize only organizationName and isSynced fields.

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

提交回复
热议问题