The use of transient keyword in Java

In the process of looking at other people's code today, I was a little confused about the keyword transient. Hey, hurry up and check the function of this keyword.

       We all know that as long as an object implements the Serilizable interface, the object can be serialized. This serialization mode of java provides a lot of convenience for developers. We do not need to concern the specific serialization process, as long as the class implements Serilizable. interface, all properties and methods of this class are automatically serialized.

       However, in the actual development process, we often encounter such problems. Some properties of this class need to be serialized, while other properties do not need to be serialized. For example, if a user has some sensitive information (such as password, bank Card number, etc.), for the sake of security, do not want to be transmitted in network operations (mainly involving serialization operations, local serialization cache is also applicable), the variables corresponding to these information can be added with the transient keyword. In other words, the lifetime of this field only exists in the caller's memory and will not be written to disk for persistence.

       In a word, the transient keyword of java provides us with convenience. You only need to implement the Serilizable interface and add the keyword transient before the attributes that do not need to be serialized. When serializing the object, this attribute will not be serialized to the specified purpose. in the ground.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
 
/**
 * @description use the transient keyword to not serialize a variable
 * Note that when reading, the order of reading data must be consistent with the order of storing data
 *        
 * @author Alexia
 * @date  2013-10-15
 */
public class TransientTest {
 
    public static void main(String[] args) {
 
        User user = new User();
        user.setUsername("Alexia");
        user.setPasswd("123456");
 
        System.out.println("read before Serializable: ");
        System.out.println("username: " + user.getUsername());
        System.err.println("password: " + user.getPasswd());
 
        try {
            ObjectOutputStream os = new ObjectOutputStream(
                    new FileOutputStream("C:/user.txt"));
            os.writeObject(user); // Write the User object to the file
            os.flush();
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace ();
        } catch (IOException e) {
            e.printStackTrace ();
        }
        try {
            ObjectInputStream is = new ObjectInputStream(new FileInputStream(
                    "C:/user.txt"));
            user = (User) is.readObject(); // Read User's data from the stream
            is.close();
 
            System.out.println("\nread after Serializable: ");
            System.out.println("username: " + user.getUsername());
            System.err.println("password: " + user.getPasswd());
 
        } catch (FileNotFoundException e) {
            e.printStackTrace ();
        } catch (IOException e) {
            e.printStackTrace ();
        } catch (ClassNotFoundException e) {
            e.printStackTrace ();
        }
    }
}
 
class User implements Serializable {
    private static final long serialVersionUID = 8294180014912103005L;  
 
    private String username;
    private transient String passwd;
 
    public String getUsername() {
        return username;
    }
 
    public void setUsername(String username) {
        this.username = username;
    }
 
    public String getPasswd() {
        return passwd;
    }
 
    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }
 
}

 The output is:

read before Serializable:
username: Alexia
password: 123456
 
read after Serializable:
username: Alexia
password: null

 From: http://www.importnew.com/21517.html

Guess you like

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