Latest Code Tutorials

Transient Keyword In Java: The Complete Guide

In Java, we have a keyword ‘transient‘ used to ignore the mentioned variables from getting serialized. This means that when we write our data in a file, there may be some data that we do not want to be written in the file. So, while writing the object in a file, the data members of that object declared as the compiler will ignore transient.

But some value must be stored at the place of that variable, or else that may create problems while reading the object from the file. So, what the compiler does is that it ignores the actual value of that variable, and the default value assigned to that variable will be stored in the file.

Transient Keyword In Java

The modifier transient in Java can be applied to the field members of the class to turn off serialization on these field members. Every field marked as a transient will not be serialized.

You use a transient keyword to indicate to the java virtual machine that the transient variable is not part of the object’s persistent state.

The transient keyword plays a vital role in meeting the security constraints. There are numerous real-life examples where we don’t want to store the private data in the file.

Another use case of the transient keyword is not to serialize a variable whose value can be calculated based on other serialized objects or systems such as a person’s age, current date, etc.

Important points to note

The transient keyword is used during serialization.

It is used for the variables which we do not want to serialize.

To make your variable transient, your class must implement the Serializable interface.

Using the transient keyword with the static or final variables is of no use. However, an error is not thrown by the compiler in such cases.

It helps maintain the privacy of confidential data.

For example, refer to the following program code.


class Login_Details implements Serializable {
    String name;
    int age;
    String email;
    transient String password;

    public Login_Details(String name, int age, String email, String password) { = name;
        this.age = age; = email;
        this.password = password;

public class Transient1 {
    public static void main(String[] args) throws Exception {
        Login_Details obj = new Login_Details("Harry", 18, "", "TRANSIENT");
        FileOutputStream login_details = new FileOutputStream("login_details.txt");
        ObjectOutputStream out = new ObjectOutputStream(login_details);
        System.out.println("Details saved");

See the output.

Transient Keyword In Java Example

Transient Keyword

See the other program.


public class Transient2 {
    public static void main(String[] args) throws Exception {
        ObjectInputStream in = new ObjectInputStream((new FileInputStream("login_details.txt")));
        Login_Details obj = (Login_Details) in.readObject();
        System.out.println("NAME: " +;
        System.out.println("AGE: " + obj.age);
        System.out.println("EMAIL: " +;
        System.out.println("PASSWORD: " + obj.password);

See the output.


Transient with final

I am talking about using transient with a final keyword correctly because it behaves differently in different situations, which is not generally the case with other keywords in Java.

For making this concept practical, I have modified the Employee class as below:

private String           firstName;
private String           lastName;

public final transient String confidentialInfo = "confidential";

public final transient Logger logger = Logger.getLogger("data");

That’s it for this tutorial.

Recommended Posts

Java Set

Java Continue Statement

Java BufferedReader Class

Java Vector Class

Java Anonymous Inner Class

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.