Java Transient Keyword
An object in Java can be turned into a stream of bytes using serialization. The data of the instance and the kind of data saved in that instance are both included in the byte stream. Deserialization does the exact opposite task. The byte sequence is changed into the original object data. When we don't want an object to be serialized during serialization, we can use a temporary keyword.
A variable modifier used in serialization is transitory. The temporary keyword is used during serialization if we don't want to save the value of a certain variable in a file. When JVM encounters a temporary keyword, it discards the variable's original value and saves the default value for that variable's data type.
The use of temporary keywords is crucial for observing security restrictions. There are many situations in real life where we don't want to save private information in a file. The transient keyword can also be used to avoid serializing variables whose values can be calculated or deduced from other serialized objects or systems, such as a person's age, the current date, etc.
Practically, we only serialized the fields that represented an instance's state; after all, the entire point of serialization is to store an object's information to a file. Use of the transitory keyword with a class's private, confidential fields during serialization is a recommended practice.
Usage of Transient Keyword
To prevent serialization of a class's data members, use the temporary keyword with them. For instance, if a programmed accepts the username and password of a user. The original password, however, should not be kept in the file. Here, we can use the transitory keyword, which instructs JVM to ignore the object's original value and instead store the object's default value when it reads the keyword.
Syntax:
private transient <variable>;
Or
transient private <variable>;
When to use the transient keyword:
- When there are data members that were derived from other data members inside the same class instance, the transitory modifier might be utilized.
- This temporary keyword can be applied to data members that don't show the object's current state.
- A temporary modification can be applied to the data members of a non-serialized object or class.
Examples of Transient Keyword
Take the Student class, for instance, which has the three data members’ id, name, and age. All the values in the object will be serialized if you serialized it, but if there are any variables, like age, that you don't want to serialize, you can define the age data member to be transitory. Two classes, Student and Persist Example, have been defined for this example. Since the Student class's age data component is marked as temporary, its value will not be serialized.
You will receive the transitory variable's default value if you deserialize the object.
Let's make a class that contains temporary variables.
import java.io. *;
public class Stud implements Serial {
int roll;
String name;
transient int ag;//here it is unable to be serialized
public Stud(int roll, String name,int ag) {
this. roll = roll;
this.name = name;
this.ag=ag;
}
}
class Persist Ex {
public static void main(String s[])throws Exception{
Stud st1 =new Stud(211,"ravi",22);//object is created
//writing object into file
FileOutStream ss=new FileOutStream("ss.txt");
ObjectOutStream out=new ObjectOutStream(ss);
Out. WriteObj(st1);
out. Flush();
out. Close();
ss. close();
System.out.println("success");
}
}
Output:
Success
Static fields are not a part of an object's state, hence using the transitory keyword with static variables serves no purpose or has no negative effects. There isn't a compilation error, though.
Transient and final variables are not used or affected by each other since final variables are directly serialized by their values. However, there isn't a compile-time error.
Create the deserialization code: import java.io. *;
class Depersis {
public static void main (String s []) throws Exception {
Object In Stream inp=new Object inStream (new FileinStream("ss.txt"));
Stud ss=(Stud)inp. readObject ();
System.out.println(ss. roll+" "+ss.name+" "+ss.ag);
in. close ();
}
}
Output:
211 Ravi 0
As you can see, printing the student's age yields a value of 0 because age was not serialized.
The use of the transitory keyword in Java, appropriate contexts for its use, and how to apply it in Java programmers have all been covered in this article.