Java serialization-keyword transient

Original author: mice love rice

Original address: A detailed summary of the transient keyword in Java

1. Brief introduction

      This article will introduce the transient keyword in Java. Transient means transient. The transient modified member variables will be ignored in the serialization process of the instance object of the class. Therefore, transient variables will not run through the serialization and deserialization of the object, and the life cycle is only stored in the memory of the caller and will not be written to the disk for persistence.

(1) Serialization

      The serialization of objects in Java refers to the conversion of objects into byte sequences. These byte sequences contain the data and information of the object. A serialized object can be written to a database or file, and Can be used for network transmission. Generally, when we use the cache (not enough memory space may be stored locally to the hard disk) or remote call rpc (network transmission), we often need to make the entity class implement the Serializable interface, the purpose is to make it serializable. Of course, the ultimate goal after serialization is to deserialize and restore the original Java object instance. Therefore, the serialized byte sequence can be restored to a Java object, and this process is deserialization.

(2) Why use the transient keyword?

      When persisting objects, for some special data members (such as user passwords, bank card numbers, etc.), we do not want to use serialization mechanisms to save them. In order to turn off serialization on a member variable of a specific object, you can add the keyword transient before the member variable.

(3) The role of transient

      transient is a keyword of the Java language, used to indicate that a member variable is not part of the serialization of the object. When an object is serialized, the value of the transient variable is not included in the serialized result. Non-transient variables are included. Note that static modified static variables are naturally not serializable.

Two, transient use summary

(1) Once a variable is transiently modified, the variable will no longer be a part of the object's persistence, and the variable content cannot be accessed after serialization.

(2) The transient keyword can only modify variables, 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 needs to implement the Serializable interface.

(3) A static variable cannot be serialized regardless of whether it is modified by transient (if the static variable in the class still has a value after deserialization, the value is the value of the corresponding static variable in the current JVM). Serialization saves the object state, and static variables save the class state, so serialization does not save static variables.

Three, usage scenarios

(1) The field value in the class can be derived from other fields. For example, a rectangular class has three attributes length, width, and area, and the area does not need to be serialized.

(2) Some security information cannot leave the JVM in general.

(3) If the Logger instance is used in the class, the Logger instance does not need to be serialized

Guess you like

Origin blog.csdn.net/sanmi8276/article/details/112800773