Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconSoftware Developmentbreadcumb forward arrow iconTransient in java: What is, How Does it Work?

Transient in java: What is, How Does it Work?

Last updated:
22nd Feb, 2023
Views
Read Time
3 Mins
share image icon
In this article
Chevron in toc
View All
Transient in java: What is, How Does it Work?

What is Transient in java?

Transient in java is a variable modifier used to avoid serialization. In other words, if an object of a data structure is marked as transient, then the serialization of data structure will not happen.  In simpler words, the transient keyword is used if the value of a specific keyword is not to be stored in a file during serialization. Therefore, the variable’s original value is ignored and the default value of its data type is saved when a transient keyword is encountered by JVM. 

In real-life examples, there are times when private and confidential data is not to be saved in a file. It is, therefore, at this point, the transient keyword plays an important role in order to meet the security constrictions. During serialization, it is always good to use transient keywords with private confidential fields.

Check out our free technology courses to get an edge over the competition.

What is the importance of transient in Java keyword?

By default, all of the object’s variables get stored as bytes in a file or are transferred across the network. In some cases, one wants to avoid persisting with some variables as they are not required. In that circumstance, one can declare these variables as transient. This is the main purpose of the transient keyword.

Ads of upGrad blog

Check Out upGrad’s Software Development Courses

In-Demand Software Development Skills

What is Serialization and Deserialization in Java?

Serialization is a mechanism that converts an object into a stream of Byte. In contrast, the opposite is Deserialization, where the stream of Bytes creates the actual Java object in the memory.

We can well understand the process given in the image given below. (Image courtesy: Google Search). It is clear here that Object gets converted in Byte stream during Serialization and during Deserialization stream of Bytes recreates the Java’s actual object in the memory.

Read our Popular Articles related to Software Development

When and where is the transient keyword used in java?

Let us now understand when and where we should be using transient in java.

  1. To be implemented when we want secure information not to leak outside.
  2. At times when there is no sense to serialize some fields.
  3. At such times when there are fields that are calculated or derived from other fields.

Let’s try and write a very basic example to understand what exactly the above analogy means. (Image courtesy: Google Search)

Ads of upGrad blog

 

Some more related terms and their definitions:

  1. A transient field is a field that is modified with the transient keyword.
  2. Transient fields are important as they have some control over the serialization process and also helps in excluding some object properties in this process.
  3. A transient variable is a variable that may not be serialized.
  4. Serialization can be done as many times as is required. The result would be the same.
  5. A field that is declared with a transient modifier will not take part in the serialized process.
  6. If one wants to avoid variable data being written to a file, one should make that variable transient.
  7. The meaning of the word transient by Google is impermanent or lasting for a short time.
  8. So, if you want to make anything impermanent, use transient in java keyword.

At upGrad, we understand the importance of practical, hands-on learning – especially when it comes to software development. As a result, our courses and training initiatives have practicality at their very core. One such initiative is Full Stack Development Bootcamp which will help you develop all the relevant skills required to excel in full-stack development. 

Profile

Pavan Vadapalli

Blog Author
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working on solving problems of scale and long term technology strategy.