Table of contents
1.
Introduction
2.
Transient Keyword in Java
3.
How to Use Transient Keyword in Java?
3.1.
1. Declare a Variable as Transient:
3.2.
2. Serialization and Deserialization:
4.
When to use the transient keyword?
5.
Where to Use the Transient Keyword?
6.
Syntax of Transient Keyword
7.
Example of Java Transient Keyword
7.1.
Implementation  
7.2.
Java
8.
Transient with Final Keyword
8.1.
Implementation   
8.2.
Java
9.
Transient with Static Keyword
9.1.
Example:   
9.2.
Java
10.
Benefits of using Transient Keyword in Java
11.
Difference Between Transient and Volatile Keyword in Java
12.
Frequently Asked Questions
12.1.
What is volatile and transient in Java?
12.2.
What is transient vs static in Java?
12.3.
Why use transient in Java?
12.4.
What is the use of the Transient keyword in Java?
12.5.
What are transient and static keyword in Java?
12.6.
What is the effect of using the Transient keyword with the Static Keyword?
13.
Conclusion
Last Updated: Jul 25, 2025
Medium

Transient Keyword in Java

Author Jay Dhoot
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Transient keywords are generally used with data members that contain confidential information. In this article, we will learn about transient keywords in Java. We will also see the syntax and usage of the keyword.

Transient Keyword in JAVA

Transient Keyword in Java

The transient keyword is used when we do not want the variable to be serialized, i.e., we do not want the value of that variable to be stored in a file. Whenever JVM sees the transient keyword, it ignores the original value of the value and just stores the default value of the same.

Serialization and De-Serialization

How to Use Transient Keyword in Java?

In Java, the transient keyword is used in the context of serialization to indicate that a variable should not be included in the serialization process. Here's how to use it:

1. Declare a Variable as Transient:

import java.io.Serializable;
public class MyClass implements Serializable {
   private transient int transientVar;
   private String nonTransientVar;
   // Constructors, methods, etc.
}

 

Marking a variable as transient in a class that implements Serializable means that the variable won't be included when the object is serialized.

2. Serialization and Deserialization:

import java.io.*;
public class SerializationExample {
   public static void main(String[] args) {
       // Serialization
       try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("myObject.ser"))) {
           MyClass myObject = new MyClass();
           oos.writeObject(myObject);
           System.out.println("Object Serialized");
       } catch (IOException e) {
           e.printStackTrace();
       }
       // Deserialization
       try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("myObject.ser"))) {
           MyClass deserializedObject = (MyClass) ois.readObject();
           System.out.println("Object Deserialized");
       } catch (IOException | ClassNotFoundException e) {
           e.printStackTrace();
       }
   }
}

 

When the object is serialized, the transientVar won't be included in the resulting byte stream. Upon deserialization, the transientVar will be assigned its default value.

When to use the transient keyword?

We can use the transient keyword when:

  • Sensitive Data: Use transient for variables that contain sensitive information (e.g., passwords) to avoid exposing them during serialization.
     
  • Derived Data: If a variable can be derived or calculated from other variables, making it transient can reduce the amount of data transmitted or stored.
     
  • Caching: For variables that can be recalculated or fetched from a cache, marking them as transient avoids unnecessary serialization and deserialization overhead.
     
  • Static or Non-Serializable Fields: Variables declared as static or non-serializable (e.g., file handles) can be marked as transient to exclude them from serialization.
     
  • Performance Optimization: Use transient for large objects or variables that don't need to be persisted, optimizing the serialization process.

Where to Use the Transient Keyword?

There are various real-life examples where we do not want to save private data like passwords, atm pins, etc., and this is where the transient keyword plays an important role. The transient keyword is generally used with the variables that need to be kept secure or with the ones whose value can be derived from other serialized objects. For example, the age of a person.

Consider the example of an object where we have a password stored in a variable. While serializing the object in a byte stream, we don't want the password to be stored on the hard disk. So for the password variable, we can use the transient keyword as it would avoid serialization of that particular variable. Whenever JVM would come across the transient keyword, it would ignore the original value of the password and would just store the default value of the same.

Syntax of Transient Keyword

The transient keyword can be used by just writing the transient keyword while defining a variable.  

For example:

class tran{
	//code 
	transient Int pin;
	//code
}


It is a good practice to use the transient keyword with the private keyword while storing private, confidential information.

Syntax:

transient private <variable>;  


Or 

private transient <member variable>;    

Example of Java Transient Keyword

Consider the example of a Ninja class with three data members: name, email, and password. As the password needs to be kept secure, we have used the transient keyword. Use of the transient keyword would not allow the password to be serialized, so a default value would be stored.

The below code implements a class Ninja, and it first serializes and then deserializes the Ninja object.

Implementation  

 

  • Java

Java

import java.io.*;      
public class Ninja implements Serializable{  
 
String name;
String email;
//Will not be serialized

transient String password;

//Constructor
//Initializes the object
public Ninja(String name, String email, String password) {  
  this.name = name;   
  this.email = email;   
  this.password=password;   
}

public static void main(String args[])throws Exception{
  //New Student Object
  Ninja C =new Ninja("Ninja1","coding.ninja@gmail.com","CN!@1");

  //Serialization  
  FileOutputStream cn=new FileOutputStream("cn.txt");   
  ObjectOutputStream out=new ObjectOutputStream(cn);   
  out.writeObject(C);   
  out.flush();   
  out.close();   
  cn.close();  
   
  System.out.println("Serialized Student Object");

  //Deserialization
  ObjectInputStream in=new ObjectInputStream(new FileInputStream("cn.txt"));   
  Ninja c=(Ninja)in.readObject(); 
 
  System.out.println("Deserialized Student Object:");
  System.out.println("Name: "+c.name+" ;"+"E-mail: " +c.email+" ;"+"Password: "+c.password);   
 
  in.close();
}
}  
You can also try this code with Online Java Compiler
Run Code


Output

Output

Transient with Final Keyword

Variables with the final keyword are Constant. Their values can not be changed after initialization. The Transient keyword can be combined with the Final keyword. The final variables can be initialized in either of the two ways:

  1. Explicit declaration: In this, the variables are initialized at the time of their declaration. Using the transient keyword with the final keyword has no effect here, as during the serialization process, the values are directly serialized. Consider the example below, we have declared the variable age explicitly, and in the output, we can see that its value is restored.
     
  2. Parameterized constructor: In this, the variables are declared inside the constructor. When serialization of the object occurs, all final transient variables will not be serialized, and after the deserialization process, these final transient variables will be assigned their default values. Consider the example below, we have declared the variable password in the parameterized constructor, and in the output, we can see that it gets restored to its default value(which is null in this case).


For example,

Implementation   

  • Java

Java

import java.io.*;      
public class Ninja implements Serializable{   
String name;
String email;

//Explicit Declaration
//Will be serialized
final transient int age=18;

//Parametrized Declaration
//Will not be serialized
final transient String password;

//Constructor
//Initializes the object
public Ninja(String name, String email, String password) {  
  this.name = name;   
  this.email = email;   
  this.password=password;   
}

public static void main(String args[])throws Exception{
  //New Student Object
  Ninja C =new Ninja("Ninja1","coding.ninja@gmail.com","CN!@1");

  //Serialization  
  FileOutputStream cn=new FileOutputStream("cn.txt");   
  ObjectOutputStream out=new ObjectOutputStream(cn);   
  out.writeObject(C);   
  out.flush();   
  out.close();   
  cn.close();   
 
  System.out.println("Serialized Student Object \n");

  //Deserialization
  ObjectInputStream in=new ObjectInputStream(new FileInputStream("cn.txt"));   
  Ninja c=(Ninja)in.readObject(); 
 
  System.out.println("Deserialized Student Object: \n");
  System.out.println("Name: "+c.name+" ;"+"E-mail: " +c.email+" ;"+"Password: "+c.password +" ;"+"Age: "+c.age+"\n");
  System.out.println("Varibale Age gets serialised as it is declared explicitly");
  System.out.println("Variable password does not get serialized and is restored with its default value.");
 
  in.close();
}
You can also try this code with Online Java Compiler
Run Code


Output

Output

Transient with Static Keyword

The Transient keyword can be combined with the Static keyword. As static variables are not a part of the object's state, there is no impact of using the transient keyword with them, and their default value is restored.
In the example below, the variable school has the static and the transient keyword; however, in the output, we can see that its default value is restored.

Example:   

  • Java

Java

import java.io.*;      
public class Ninja implements Serializable{   
String name;
String email;

//No impact of transient here
static transient String school="CN";

//password will be serialized
transient String password;

//Constructor
//Initializes the object
public Ninja(String name, String email, String password) {  
  this.name = name;   
  this.email = email;   
  this.password=password;   
}

public static void main(String args[])throws Exception{
  //New Student Object
  Ninja C =new Ninja("Ninja1","coding.ninja@gmail.com","CN!@1");

  //Serialization  
  FileOutputStream cn=new FileOutputStream("cn.txt");   
  ObjectOutputStream out=new ObjectOutputStream(cn);   
  out.writeObject(C);   
  out.flush();   
  out.close();   
  cn.close();   
 
  System.out.println("Serialized Student Object \n");

  //Deserialization
  ObjectInputStream in=new ObjectInputStream(new FileInputStream("cn.txt"));   
  Ninja c=(Ninja)in.readObject(); 
 
  System.out.println("Deserialized Student Object: \n");
  System.out.println("Name: "+c.name+" ;"+"E-mail: " +c.email+" ;"+"Password: "+c.password +" ;"+"School: "+c.school+"\n");
  System.out.println("Varibale School has no impact of transient keyword and returns its default value");
 
  in.close();
}
You can also try this code with Online Java Compiler
Run Code


Output

Output

 

Benefits of using Transient Keyword in Java

There are many benefits of using the transient keyword in Java:

  1. Improved Security: Transient keyword helps in preventing the Serialization of the objects and also prevents storing their values. This helps prevent unauthorized access to sensitive data like passwords, atm PINs, etc.
     
  2. Faster Performance: The use of transient keywords prevents the Serialization of variables, thereby making the serialization process faster as fewer data need to be serialized.
     
  3. Reduced Size of Byte Stream: In Serialization, an object gets converted into a byte stream. As transient variables are not serialized, the byte stream's size is considerably reduced.

Difference Between Transient and Volatile Keyword in Java

Featuretransient Keywordvolatile Keyword
PurposeUsed in the context of serialization.Ensures visibility and atomicity of a variable's value in a multithreaded environment.
UsageApplied to instance variables in a class.Applied to instance variables.
SerializationExcludes the variable from the serialization process.No specific impact on serialization.
Thread SafetyDoes not address thread safety.Ensures visibility but does not guarantee atomicity for compound operations.
Visibility GuaranteeDoes not guarantee visibility or atomicity in a multithreaded environment.Ensures visibility and atomicity for simple read and write operations.
Use CasePrevents certain variables from being serialized (e.g., sensitive information).Ensures the latest value of a variable is visible to all threads.
Exampleprivate transient int sensitiveData;private volatile boolean flag;

Frequently Asked Questions

What is volatile and transient in Java?


Volatile ensures visibility of changes to variables across threads, while transient prevents variables from being serialized during object serialization.

 

What is transient vs static in Java?


Transient skips variable serialization, whereas static belongs to the class, not the object. Both are ignored during serialization by default.

 

Why use transient in Java?


Transient is used to exclude sensitive or irrelevant data from serialization, reducing file size and improving data security.

What is the use of the Transient keyword in Java?

 

The transient keyword in Java prevents a variable from getting serialized. Whenever Java Virtual Machine(JVM) sees the transient keyword, it ignores its original value and just stores the default value of the same.

What are transient and static keyword in Java?

 

transient keyword used in serialization. It marks a variable not to be serialized. It prevents sensitive or unnecessary data from being persisted. On the other hand, static keyword is associated with class rather than instances. It is shared among all instances. It is used for constants, methods, or shared resources.

What is the effect of using the Transient keyword with the Static Keyword?

 

The Transient keyword can be combined with the Static keyword. As static variables are not a part of the object's state, there is no impact of using transient keywords with them and its default value is restored.

Conclusion

In this article, we explored the Transient Keyword in Java with practical examples and use cases. The Java Transient Keyword plays a crucial role in securing sensitive data during serialization by ensuring certain variables are not persisted. Understanding when and how to use transient effectively can improve both application performance and data security.

Recommended Readings:

Live masterclass