Transient keyword, javatransient keyword
The transient keyword is transient. It can be seen that it is instantaneous and cannot be fixed.
Will it be related to the object status and so on? I found some information online, which is related to object serialization.
As long as an object implements the Serilizable interface, this object can be serialized. This serialization mode of java provides developers with a lot of convenience, so we don't have to worry about the specific serialization process, as long as this class implements the Serilizable interface, all attributes and methods of this class will be automatically serialized.
In the actual development process, we often encounter this problem. Some attributes of this class need to be serialized, while other attributes do not need to be serialized. For example, if a user has some sensitive information (such as passwords and bank card numbers), the user does not want to operate on the network for security reasons (mainly involves serialization operations, and the local serialization cache is also applicable) the transient keyword can be added to the variables corresponding to the information. In other words, the lifecycle of this field only exists in the caller's memory and is not written to the disk for persistence.
In short, the transient keyword of java provides convenience for us. You only need to implement the Serilizable interface and add the transient keyword before the attribute that does not need to be serialized. When serializing the object, this attribute will not be serialized to the specified destination.
Example:
1 public class TransientTest {2 3 public static void main (String [] args) {4 5 User user = new User (); 6 user. setUsername ("lucy"); 7 user. setPasswd ("123456"); 8 9 System. out. println ("read before Serializable:"); 10 System. out. println ("username:" + user. getUsername (); 11 System. err. println ("password:" + user. getPasswd (); 12 13 try {14 ObjectOutputStream OS = new ObjectOutputStream (15 new FileOutputStream ("C:/user.txt"); 16 OS. writeObject (user); // write the User object to file 17 OS. flush (); 18 OS. close (); 19} catch (FileNotFoundException e) {20 e. printStackTrace (); 21} catch (IOException e) {22 e. printStackTrace (); 23} 24 try {25 ObjectInputStream is = new ObjectInputStream (new FileInputStream (26 "C:/user.txt"); 27 user = (User) is. readObject (); // read User data from the stream 28 is. close (); 29 30 System. out. println ("\ nread after Serializable:"); 31 System. out. println ("username:" + user. getUsername (); 32 System. err. println ("password:" + user. getPasswd (); 33 34} catch (FileNotFoundException e) {35 e. printStackTrace (); 36} catch (IOException e) {37 e. printStackTrace (); 38} catch (ClassNotFoundException e) {39 e. printStackTrace (); 40} 41} 42} 43 44 class User implements Serializable {45 private static final long serialVersionUID = 8294180014912103005L; 46 47 private String username; 48 private transient String passwd; 49 50 public String getUsername () {51 return username; 52} 53 54 public void setUsername (String username) {55 this. username = username; 56} 57 58 public String getPasswd () {59 return passwd; 60} 61 62 public void setPasswd (String passwd) {63 this. passwd = passwd; 64} 65 66}
Output result:
1 read before Serializable: 2 username: lucy3 password: 1234564 5 read after Serializable: 6 username: lucy7 password: null
1. Once a variable is modified by transient, the variable is no longer a part of Object Persistence and cannot be accessed after serialization.
2. The transient keyword can only modify variables, but not methods and classes. Note that local variables cannot be modified by the transient keyword. If the variable is a user-defined class variable, the class must implement the Serializable interface.
3. Variables modified by the transient keyword can no longer be serialized. A static variable cannot be serialized no matter whether it is modified by the transient or not.
The following code:
1 public class TransientTest {2 3 public static void main (String [] args) {4 5 User user = new User (); 6 user. setUsername ("lucy"); 7 user. setPasswd ("123456"); 8 9 System. out. println ("read before Serializable:"); 10 System. out. println ("username:" + user. getUsername (); 11 System. err. println ("password:" + user. getPasswd (); 12 13 try {14 ObjectOutputStream OS = new ObjectOutputStream (15 new FileOutputStream ("C:/user.txt"); 16 OS. writeObject (user); // write the User object to file 17 OS. flush (); 18 OS. close (); 19} catch (FileNotFoundException e) {20 e. printStackTrace (); 21} catch (IOException e) {22 e. printStackTrace (); 23} 24 try {25 // change the username value 26 User before deserialization. username = "jack"; 27 28 ObjectInputStream is = new ObjectInputStream (new FileInputStream (29 "C:/user.txt"); 30 user = (User) is. readObject (); // read User data from the stream 31 is. close (); 32 33 System. out. println ("\ nread after Serializable:"); 34 System. out. println ("username:" + user. getUsername (); 35 System. err. println ("password:" + user. getPasswd (); 36 37} catch (FileNotFoundException e) {38 e. printStackTrace (); 39} catch (IOException e) {40 e. printStackTrace (); 41} catch (ClassNotFoundException e) {42 e. printStackTrace (); 43} 44} 45} 46 47 class User implements Serializable {48 private static final long serialVersionUID = 8294180014912103005L; 49 50 public static String username; 51 private transient String passwd; 52 53 public String getUsername () {54 return username; 55} 56 57 public void setUsername (String username) {58 this. username = username; 59} 60 61 public String getPasswd () {62 return passwd; 63} 64 65 public void setPasswd (String passwd) {66 this. passwd = passwd; 67} 68 69}
Output result:
read before Serializable: username: lucypassword: 123456read after Serializable: username: jackpassword: null
This indicates that the value of the static variable username in the Post-deserialization class is the value of the static variable corresponding to the current JVM, rather than the value obtained through deserialization.
1 public class ExternalizableTest implements Externalizable {2 3 private transient String content = "Yes, I will be serialized, whether or not I have been modified by the transient keyword "; 4 5 @ Override 6 public void writeExternal (ObjectOutput out) throws IOException {7 out. writeObject (content); 8} 9 10 @ Override11 public void readExternal (ObjectInput in) throws IOException, 12 ClassNotFoundException {13 content = (String) in. readObject (); 14} 15 16 public static void main (String [] args) throws Exception {17 18 ExternalizableTest et = new ExternalizableTest (); 19 ObjectOutput out = new ObjectOutputStream (new FileOutputStream (20 new File ("test"); 21 out. writeObject (et); 22 23 ObjectInput in = new ObjectInputStream (new FileInputStream (new File (24 "test"); 25 et = (ExternalizableTest) in. readObject (); 26 System. out. println (et. content); 27 28 out. close (); 29 in. close (); 30} 31}
Output result:
Yes, I will be serialized, whether or not I have been modified by the transient keyword
In Java, Object serialization can be achieved through the implementation of two interfaces. If the Serializable interface is implemented, all serialization will be performed automatically. If the Externalizable interface is implemented, nothing can be automatically serialized. You need to manually specify the variable to be serialized in the writeExternal method, which is irrelevant to whether the variable is modified by transient. Therefore, the second example outputs the content initialized by the variable content instead of null.