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.

Explore Free Courses

Suggested Tutorials

View All

Suggested Blogs

Best Jobs in IT without coding
134098
If you are someone who dreams of getting into the IT industry but doesn’t have a passion for learning programming, then it’s OKAY! Let me
Read More

by Sriram

12 Apr 2024

Scrum Master Salary in India: For Freshers & Experienced [2023]
900289
Wondering what is the range of Scrum Master salary in India? Have you ever watched a game of rugby? Whether your answer is a yes or a no, you might h
Read More

by Rohan Vats

05 Mar 2024

SDE Developer Salary in India: For Freshers & Experienced [2024]
904971
A Software Development Engineer (SDE) is responsible for creating cross-platform applications and software systems, applying the principles of compute
Read More

by Rohan Vats

05 Mar 2024

System Calls in OS: Different types explained
5019
Ever wondered how your computer knows to save a file or display a webpage when you click a button? All thanks to system calls – the secret messengers
Read More

by Prateek Singh

29 Feb 2024

Marquee Tag & Attributes in HTML: Features, Uses, Examples
5127
In my journey as a web developer, one HTML element that has consistently sparked both curiosity and creativity is the venerable Marquee tag. As I delv
Read More

by venkatesh Rajanala

29 Feb 2024

What is Coding? Uses of Coding for Software Engineer in 2024
5046
Introduction  The word “coding” has moved beyond its technical definition in today’s digital age and is now considered an essential ability in
Read More

by Harish K

29 Feb 2024

Functions of Operating System: Features, Uses, Types
5112
The operating system (OS) stands as a crucial component that facilitates the interaction between software and hardware in computer systems. It serves
Read More

by Geetika Mathur

29 Feb 2024

What is Information Technology? Definition and Examples
5049
Information technology includes every digital action that happens within an organization. Everything from running software on your system and organizi
Read More

by spandita hati

29 Feb 2024

50 Networking Interview Questions & Answers (Freshers & Experienced)
5123
In the vast landscape of technology, computer networks serve as the vital infrastructure that underpins modern connectivity.  Understanding the core p
Read More

by Harish K

29 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon