This blog will elaborate on the usage and implementation of the “transient” keyword in Java.
What is the “transient” Keyword in Java?
The Java “transient” keyword is utilized to avoid “serialization”. If any object of a data structure is allocated as a transient, it means that it will not be serialized. It works such that if a particular variable is allocated as transient, it cannot be written in the file, and “0” is stored instead.
Syntax
In this syntax, “mem var” points to the variable allocated as “transient”.
Before heading to the example, make sure to include the following libraries to work with the “Serializable” interface and access all the classes and methods, respectively:
import java.io.*;
Demonstration of the “transient” Keyword in Java
In the following example, the “transient” keyword can be associated with a variable such that its passed value in the object cannot be written in the file:
String name;
String city;
transient int age;
transientkey(String name, String city, int age) {
this.name = name;
this.city = city;
this.age = age;
}
According to the above lines of code, apply the below-provided steps:
- First of all, define a class named “transientkey” that implements “Serializable” to make the class object serializable and allow conversion of an object to be saved in the form of a file.
- In the class definition, specify the stated variables with the “age” assigned as “transient”.
- Now, create a class constructor having the stated parameters identical to the specified variables.
- In the constructor definition, refer to the specified variables and allocate them the parameter values via “this”.
Now, overview the “main” code snippet:
transientkey object =new transientkey("Harry","London",22);
FileOutputStream fileOut=new FileOutputStream("defaultfile.txt");
ObjectOutputStream objOut=new ObjectOutputStream(fileOut);
objOut.writeObject(object);
objOut.flush();
objOut.close();
fileOut.close();
System.out.println("Data Stored successfully!");
ObjectInputStream file=new ObjectInputStream(new FileInputStream("defaultfile.txt"));
transientkey readData=(transientkey)file.readObject();
System.out.println(readData.name+" "+readData.city+" "+readData.age+"");
file.close();
}}
In the “main”, consider the below-provided steps:
- Create a class object via the “new” keyword and the “transientkey()” constructor, respectively, and pass the stated values as constructor arguments.
- In the next step, create a “FileOutputStream” object to write the passed data to the specified file.
- After that, likewise, create an “ObjectOutputStream” object to write objects that can be read by the “ObjectInputStream”.
- Now, apply the “writeObject()” method to write the specified class object to the “ObjectOutputStream”.
- To flush and close the stream, apply the “flush()” and “close()” methods, respectively.
- To read the file, create an “ObjectInputStream” object and accumulate the written file as a “FileInputStream” parameter to fetch the input bytes from the file.
- Lastly, read the contents of the file by referring to the created class object “transientkey”.
- Algorithm: The values passed in the object will be written to the file except the value allocated as “transient”. Upon reading the file, all the values will be retrieved but the transient value will be displayed as “0”.
Entire Code
String name;
String city;
transient int age;
transientkey(String name, String city, int age) {
this.name = name;
this.city = city;
this.age = age;
}
public static void main(String args[])throws Exception{
transientkey object =new transientkey("Harry","London",22);
FileOutputStream fileOut=new FileOutputStream("defaultfile.txt");
ObjectOutputStream objOut=new ObjectOutputStream(fileOut);
objOut.writeObject(object);
objOut.flush();
objOut.close();
fileOut.close();
System.out.println("Data Stored successfully!");
ObjectInputStream file=new ObjectInputStream(new FileInputStream("defaultfile.txt"));
transientkey readData=(transientkey)file.readObject();
System.out.println(readData.name+" "+readData.city+" "+readData.age+"");
file.close();
}}
Output
In the above outcome, it can be implied that since the “age” is allocated as “transient”, therefore its value is not written in the file and retrieved accordingly i.e., “0”.
Created File
The created file is displayed below:
That was all about the usage of the transient keyword in Java.
Conclusion
The Java transient keyword is utilized to refrain from serialization such that if a particular variable is assigned as “transient”, it cannot be written in the file, and “0” is stored instead. This article discussed the usage and implementation of the “transient” keyword in Java.