Transient in java: What is, How Does it Work?
Updated on Mar 28, 2025 | 3 min read | 5.6k views
Share:
For working professionals
For fresh graduates
More
Updated on Mar 28, 2025 | 3 min read | 5.6k views
Share:
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.
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.
Check Out upGrad’s Software Development Courses
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.
Let us now understand when and where we should be using transient in java.
Let’s try and write a very basic example to understand what exactly the above analogy means. (Image courtesy: Google Search)
Some more related terms and their definitions:
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.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources