AppDividend
Latest Code Tutorials

Transient Keyword In Java Example | Java Transient Keyword Tutorial

0

Transient Keyword In Java Example | Java Transient Keyword Tutorial is today’s topic. In Java, we have a keyword ‘transient’ which is used to ignore the mentioned variables form getting serialized. What this means is that, when we write our data in a file, then 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 which are declared as transient will be ignored by the compiler. But some value must be stored at the place of that variable or else that may create problem 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 a persistent state of the object.

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

Another usecase of transient keyword is not to serialize a variable whose value can be calculated based on other serialized objects or system such as an age of a person, 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 variables which are static or final 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.

import java.io.*;

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) {
        this.name = name;
        this.age = age;
        this.email = email;
        this.password = password;
    }
}

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

See the output.

 

 

Transient Keyword In Java Example

Transient Keyword

See the other program.

import java.io.*;

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: " + obj.name);
        System.out.println("AGE: " + obj.age);
        System.out.println("EMAIL: " + obj.email);
        System.out.println("PASSWORD: " + obj.password);
    }
}

See the output.

 

Transient

Transient with final

I am talking about the use of transient with a final keyword correctly because it behaves differently in different situations which are 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");

Finally, Transient Keyword In Java Example | Java Transient Keyword Tutorial is over.

Recommended Posts

Java Set Example | Set Example In Java Tutorial

Java Continue Statement Example | Continue Statement Tutorial

Java BufferedReader Class Tutorial With Example

Java Vector Class Tutorial With Example From Scratch

Java Anonymous Inner Class Tutorial With Example

Leave A Reply

Your email address will not be published.

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