Transient in Java | What, Why and How it works | Edureka

Transient in Java is used to mark the member variable not to be serialized when it is persisted to streams of bytes. This keyword plays an important role to meet security constraints in Java. It ignores the original value of a variable and saves the default value of that variable data type.

Following are the topics that will be discussed in this article:

Let’s get started!

What is Transient keyword in Java?

Transient is basically a variables modifier that used for serialization. Now, what is Serialization? Serialization in Java is a mechanism that is used in converting the state of an object into a byte stream. At the time of serialization, if you don’t want to save the value of a particular variable in a file, then use the transient keyword.

Syntax:

private transient <member variable>;

or

 transient private <member variable>; 

In case you define any data member as transient, it will not be serialized. This is because every field marked as transient will not be serialized. You can use this transient keyword to indicate the Java virtual machine (JVM) that the transient variable is not part of the persistent state of an object.

Let’s write a very basic example to understand about Transient in Java.

 class Demo implements Serializable
{
// Making human transient
private transient String human;
transient int age;
// serialize other fields
private String name, address;
Date dob;
// rest of the code
}

Here, I created a class called Demo which implements Serializable. The age data member of the Demo class is declared as transient, its value will not be serialized. But if you deserialize the object, you will get the default value for a transient variable.

Why is Transient modifier used?

Transient in Java is used to indicate that a field should not be part of the serialization process.

The modifier Transient can be applied to member variables of a class to turn off serialization on these member variables. Every field that is marked as transient will not be serialized. You can use this transient keyword to indicate to the Java virtual machine that the transient variable is not part of the persistent state of an object.

You might have this question running in your head. When to use this Transient in Java?

The answer to this would be:

  1. You can use this Transient keyword when you have fields that are derived/calculated from other fields within the instance of a class.
  2. Use it with fields which are not marked as “Serializable” inside JDK or application code. This is because classes which do not implement Serializable interface are referenced within any serializable class and cannot be serialized and will throw “java.io.NotSerializableException” exception. Note that these non-serializable references should be marked “transient” before serializing the main class.

How to use Transient with Final keyword?

Transient in Java can be used with the final keyword because it behaves differently in different situations which is not generally the case with other keywords in Java.

Have a look at this example.

private String
firstName;
private String
lastName;

//final field 1

public final transient String pass= "password";

//final field 2

public final transient Lock lock = Lock.getLock("demo");

Now when you run the serialization (write/read) again, you’ll get this output:

Kenny
Stark
password
null

This is because We have marked the “pass” to transient, and still that field was serialized. For a similar declaration, lock was not serialized. The reason is, whenever any final field is evaluated as a constant expression, it is serialized by the JVM ignoring the presence of transient keyword.

Difference between Transient and Volatile

This is an important question asked during a Java interview. What is the difference between transient and volatile keyword in Java?

Volatile and Transient are two completely different keywords that are used in Java. A Transient keyword is used during serialization of Java object. Volatile is related to the visibility of variables modified by multiple threads.

The only similarity between these keywords is that they are less used or uncommon keywords and not as popular as public, static or final.

This brings us to the end of this article where we have learned about Transient in Java. Hope you are clear with all that has been shared with you in this tutorial.

If you found this article on “Transient in Java” relevant, check out the Edureka’s Java Course, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

If you come across any questions, feel free to ask all your questions in the comments section of “Transient in Java” and our team will be glad to answer.