Last Updated: 04 October, 2023
Prerequisite : Serialization and Deserialization in Java
In Java, the transient is a modifier used in the serialization process. At the time of serialization, if we don’t want to serialize any particular variables of a class, then we use the transient keyword with those variables. When the JVM finds a variable as a transient variable, it ignores the original value of the variable and assigns the default value of that variable's data type.
In order to meet security constraints, transient keywords are very important.
The process of converting an object into a byte stream is known as Serialization, and its reverse process is known as Deserialization in Java.
The entire process of Serialization and Deserialization is platform-independent.
Let's understand the uses of transient keywords with the help of a given sample Java program.
Example 1 : Using transient keyword in a Java Program
In the above example, Employee class has two confidential properties named loginId and password. These properties we don't want to serialize that's why they are declared as transient, which means that at the time of serialization, the JVM will assign a default value (String default value is null) to these two variables, not the actual value.
Output
Employee object successfully serialized
Deserlized Object Employee values:
EmpID: 101
Name: Ganesh
LoginID: null
Password: null
Static variables belong to a particular class, not to instances of that class. If we serialize the instance, then only instance variables will be serialized, not static variables. If we don't want to serialize the instance variable, we declare the instance variable as transient.
But if we declare transients with static variables, then there will be no impact and no use either. It will not give any compilation errors while compiling the code.
If a variable is defined as static and transient, then a static modifier will govern the behavior of the variable but not the transient.
Here is an example of transient keyword with static variables:
Example 2 : Using transient keyword with static in a Java Program
Output
Employee object successfully serialized
Deserlized Object Employee values:
EmpID: 101
Name: Ganesh
Department: Human Resource
CTC: 15.5
Total Years of Experience: 0
Using transient with the final keyword doesn’t have an impact on serialization as the variables declared final are directly serialized by their values. The JVM will assign the original value of those variables (declared transient and final, both).
Let's understand it with the help of the example given below.
Example 3 : Using transient keyword with final in a Java Program
Output
Employee object successfully serialized
Deserlized Object Employee values:
Roll No: 101
Name: Ganesh
LoginID: testLoginId
Password: null
The transient keyword is often used for fields that hold sensitive or temporary data that should persist when the object is serialized. Some common use cases include fields storing passwords, temporary caches, or fields that can be recalculated or reconstructed when needed.
It's important to note that the transient keyword only affects the serialization process. The field will still retain its value and behavior within the program itself.
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
What is the difference between transient and volatile in Java?
Ans. Transient and volatile are modifiers in Java and are used with variables only. Both are used for different purposes, but there are many differences between them. Let's understand it with the help of the table below.
Transient | Volatile |
A transient keyword is used along with instance variables to exclude them from the serialization process. If a field is transient, its value will not persist. | The volatile keyword can also be used in variables to indicate to the compiler and JVM that they always read their value from the main memory and follow a happens-before relationship on the visibility of volatile variables among multiple threads. |
Transient keywords cannot be used along with static keywords. | Volatile can be used along with static. |
During deserialization, they are initialized with a default value. | They are not initialized with a default value. |
Transient cannot be used with the final keyword. | Volatile can be used as the final keyword. |