Transient Variable in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Transient variables are special variables created using the transient keyword. An instance of this type of variable does not have its value serialized at the time of serialization. Transient variables are variables that are initialized with their default values during de-serialization.

Transient variables play an important role in preventing objects from being serialized. The transient keyword can be used to make any variable transient.

What are Serialization and De-Serialization in Java?

Serialization:

The serialization process in Java transforms an Object code into a byte stream, which can be copied from one Java Virtual Machine to another and recreated by deserialization.

De Serialization:

De Serialization transforms the byte stream into a Java object in memory.

Transient Variable in Java

Java transient variables are variables whose values are not serialized during serialization and that are initialized by default during de-serialization. For example, for an object transient variable, this behaviour can be customized using a Custom Serialized form or through a custom Externalizable interface. With a transient variable, you can prevent any object from being serialized. The transient keyword can be used to make any variable transient. A property marked as transient must has a default value during deserialization. De-serialized objects cannot be used otherwise.

How do you use a Transient Variable in Java? - with Serialization Example

The process of serializing in Java refers to converting objects into Byte Streams, which can then be transferred from one Java Virtual Machine to another and recreated by using the process of deserializing.

The following complete code example illustrates how to use transient variables in Java.

Explanation:

The following example illustrates serializing objects in Java. You will notice that transient variables are not serialized and stored, but are initialized with their default values, which is zero for the integer variable.

Due to the absence of the constructor during de-serialization, it will not receive the value that was passed in during the constructor. As a result, transient variables need to be handled carefully

Output:

How to use a transient variable in Java - with Serialization Example

Why do we Need a Transient Variable in java?

Transients enable you to control how serialization will be handled, and you can omit particular properties from serialization. Certain aspects of an object may not need to be serialized depending on the context. In the following section, we will see what variables should not be serialized and transient.

Which Variable Should You Mark Transient?

Knowing how transient keywords or variables function makes determining which variables should be transient easy. From my perspective, I do not save variables that can be calculated from other variables.

It would be unnecessary to serialize, for example, a field called "area" whose value can be derived from other fields like length, breadth, height, etc.

It is not necessary to save the scores when calculating CGPA since that is done automatically when the score is deserialized.

Examples of Transient Variables in Java

In the example below, we created a simple String object of the Member class with a transient integer id provided. Due to the transient nature of the id, it cannot be written into the file, so 0 is stored instead.

Explanation:

In the above example, we print the ISBN, tile, author, and edition of the books by using the transient variables in Java.

Output:

Transient Variable in java

Explore Scaler Topics Java Tutorial and enhance your Java skills with Reading Tracks and Challenges.

Conclusion

  1. ** Transient keywords ** can only be used for ** fields ** and ** member variables **. Applying them to methods or local variables will result in a compilation error.
  2. The Java compiler does not complain when you declare a variable both static and transient at the same time, but doing that makes no sense since transients instruct the compiler not to save the variable, whereas static variables are not saved in the serialization process.
  3. Using the transient and final keywords together, you can build a variable compiler that will not complain, but you will have to reinitialize a final variable when you deserialize.
  4. Java transient variables are not saved or persistent when objects are serialized.