transient keyword

Mainly used for storage during serialization

  General fields are saved to the local disk after serialization, and can be retrieved when read from the local disk again.

 However, if the modifier transient is added to the entity class, this field will not be read when it is saved to the local disk. For example, data such as bank passwords that do not want to be written to the disk can use transient.

 

public class DXWUser implements Serializable {

private static final long serialVersionUID = 3087341266247888811L;

private String name;

private transient String password;

DXWUser(String name,String password){
this.name = name;
this.password = password;
}
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String toString(){

return name+","+password;
}
}

 

 

 


DXWUser user = new DXWUser("zhangsan","11111222");
System.out.println(user.toString());
try {
ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(new File("D:/log.out")));
o.writeObject(user);
o.close();
} catch (Exception e) {
e.printStackTrace();
}

try {
ObjectInputStream op = new ObjectInputStream(new FileInputStream(new File("D:/log.out")));
DXWUser duser = (DXWUser) op.readObject();
System.out.println(duser.toString());
} catch (Exception e) {
e.printStackTrace();

}

The two output results are

zhangsan,11111222

zhangsan,NOT SET

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324819370&siteId=291194637