Home  >  Article  >  Java  >  Summary of Java-transient usage code examples

Summary of Java-transient usage code examples

黄舟
黄舟Original
2017-03-15 11:49:221651browse

What you learn on paper is shallow, but you know you have to do it in detail --Lu You Ask the canal how clear it is to have a source of living water --Zhu Xi


transient has the meaning of "temporary" and "ephemeral". We have learned about Serializable and Java serialization. When we do not want to serialize certain variables You can set this variable to transient. Transient is a keyword in the Java language and is used to indicate that a domain is not part of the serialization of the object. transient indicates that a attribute is temporary and will not be serialized.

public class TransientDemo implements Serializable{
/**
     *
     */
private static final long serialVersionUID = 1L;
    private  transient String name;
    private String 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 static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
// TODO Auto-generated method stub  
    String path="D:"+File.separator+"object.txt";
    File file=new File(path);
    TransientDemo transientDemo=new TransientDemo();
    transientDemo.setName("姓名");
    transientDemo.setPassword("密码");
    ObjectOutput output=new ObjectOutputStream(new FileOutputStream(file));
    output.writeObject(transientDemo);
    ObjectInput input=new ObjectInputStream(new FileInputStream(file));
    TransientDemo demo=(    TransientDemo )input.readObject();
    System.out.println(demo.getName()+demo.getPassword());
    }

}

The resulting password output is

null

The above is the detailed content of Summary of Java-transient usage code examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn