Java Transient Keyword

In the world of Java programming, developers often come across terms like “transient” when dealing with variables. These modifiers play a crucial role in determining the behavior and characteristics of variables in different scenarios. In this article, we will look into the concepts of transient, explore it for better understanding.

Transient Keyword in Java

In Java, the transient keyword is used in the context of serialization to indicate that a variable should not be included in the serialization process. When applied to a variable, it instructs the Java Virtual Machine (JVM) to exclude the variable from the default serialization process. Serialization is the process of converting an object into a stream of bytes to store it in memory or transmit it over a network.

The main purpose of marking a variable as transient is to prevent its value from being persisted. This is particularly useful when dealing with sensitive or non-serializable data that should not be stored or transmitted. By excluding such variables from serialization, we can ensure the privacy and integrity of the data.

public class Student implements Serializable {
    private String name;
    private transient int age;
    // ...
}

In the above code snippet, the “age” variable is marked as transient. When an instance of the Student class is serialized, the value of the “age” variable will not be included. During de-serialization, the “age” variable will be assigned its default value (0 for integers) instead of the serialized value.

It’s important to note that only the variable itself is excluded from serialization, not the entire object. Other non-transient variables within the object will still be serialized and persisted.

Analogy: Think of a transient variable as a personal secret that you don’t want to reveal to others. It’s like a hidden piece of information that doesn’t get shared when passing an object to different environments. Just as you may choose to keep certain information private, marking a variable as transient ensures that it remains hidden during the serialization process. 

When to use the transient keyword?

We can use the transient keyword when:

  • Sensitive Data: Use transient for variables that contain sensitive information (e.g., passwords,social security number, bank account number…) to avoid exposing because of for security reasons.
  • Derived Data: If a variable can be derived or calculated from other variables, making it transient can reduce the amount of data transmitted or stored.
  • Caching: For variables that can be recalculated or fetched from a cache, marking them as transient avoids unnecessary serialization and deserialization overhead.
  • Static or Non-Serializable Fields: Variables declared as static or non-serializable (e.g., file handles) can be marked as transient to exclude them from serialization.
  • Performance Optimization: Use transient for large objects or variables that don’t need to be persisted, optimizing the serialization process.

Where to Use the Transient Keyword?

There are various real-life examples where we do not want to save private data like passwords, atm pins, etc., and this is where transient keyword play an important role. The transient keyword is generally used with the variables that are needed to be kept secured or with the ones whose value can be derived from other serialized objects. For example, the age of a person.

Consider the example of an object where we have a password stored in a variable. While serializing the object in a byte stream, we don’t want the password to be stored on the hard disk. So for the password variable, we can use the transient keyword as it would avoid serialization of that particular variable. Whenever JVM would come across the transient keyword, it would ignore the original value of the password and would just store the default value of the same.

Transient with Final Keyword

Variables with the final keyword are Constant. Their values can not be changed after initialization. The Transient keyword can be combined with the Final keyword. The final variables can be initialized in either of the two ways:

  1. Explicit declaration: In this, the variables are initialized at the time of its declaration. Using the transient keyword with the final keyword has no effect here, as during the serialization process, the values are directly serialized.Consider the below example, we have declared the variable age explicitly, and in the output, we can see that its value is restored.
  2. Parameterized constructor: In this, the variables are declared inside the constructor. When serialization of the object occurs, all final transient variables will not be serialized, and after the deserialization process, these final transient variables will be assigned their default values. Consider the below example, we have declared the variable password in the parameterized constructor, and in the output, we can see that it gets restored to its default value(which is null in this case).

Transient with Static Keyword

The Transient keyword can be combined with the Static keyword. As static variables are not a part of the object’s state, there is no impact of using transient keywords with them and its default value is restored.
In the below example, variable school has the static and the transient keyword, however in the output we can see that its default value is restored.

Benefits of using Transient Keyword in Java

There are many benefits of using the transient keyword in Java:

  1. Improved Security: Transient keyword helps in preventing the Serialization of the objects and also prevents storing their values. This helps prevent unauthorized access to sensitive data like passwords, atm PINs, etc.
  2. Faster Performance: The use of transient keywords prevents the Serialization of variables, thereby making the serialization process faster as fewer data need to be serialized.
  3. Reduced Size of Byte Stream: In Serialization, an object gets converted into a byte stream. As transient variables are not serialized, the byte stream’s size is considerably reduced.

Differences Between Transient and Volatile keyword

  • Instance variables can be excluded from serialization by using the transient keyword in conjunction with them. The volatile keyword can be used in variables to signal that the JVM and compiler consistently read the value from the main memory and follow the ‘happens-before’ relationship on the visibility of the volatile variable in between various threads.
  • Volatile keywords can be used along with static keywords, but transient keywords cannot be used.
  • During deserialization, transient variables are provided with default values, and the application code must manage their assignment or the value restoration.

Leave a Comment