Java transient Modifier
Excluding Fields from Serialization with the transient Modifier
Serialization is the process of converting an instantiated object into a byte array. It is used to store objects in files or databases, or to send and receive them over a network.
In Java, when an object is Serializable, all fields are serialized by default. However, there are cases where you do not want a field to be serialized, such as when it is a temporary variable. In such cases, you can specify the transient modifier on the field to exclude it from serialization.
class MyClass implements Serializable {
private String value;
private transient int tmp; // Excluded from serialization
}
When Is It Needed?
- Use it when you want to exclude security information such as passwords from the serialization process.
- It can be declared when you do not want to transmit data for various reasons.