Transient variable in Java
In this article, you will be acknowledged about transient variable along with its functions. We would conclude by understanding an example program about it.
Transient variable
By introducing the transitory keyword, we may construct a particular kind of variable called a transient variable. It is a specific kind of variable that, at the time of processing, has a non-serialized value. Transient variables are variables that are started by their default value during de-serialization.
In large part because of a transitory variable, an item cannot be reproduced. We are able to define any variable fleeting by utilising the transitory keyword.
Typically, the interviewer will inquire as to the distinction between a volatile and transient variable. In this way, the volatile and transient variables are distinct from one another. A volatile keyword is relevant to the accessibility of values that are updated by many threads during concurrent programming, whereas the transient keyword would be primarily used when serializing an object.
The keywords volatile and transient are both less frequently employed by programmers and less well-liked than public, static, or final.
We make advantage of the temporary keyword to keep instance variables out of the serialization process. We just use volatile keyword to tell the compiler to retrieve the state of the variables from main memory.
The temporary keyword cannot be used with static, while the volatile keyword can.
Let us understand how this works with an example program
File name: Transient.java
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.ObjectInputStream;
importjava.io.ObjectOutputStream;
importjava.io.Serializable;
//Initiating a class with name Transient for understanding the functionality of transient //variable
public class Transient {
//The below is the main method
public static void main(String args[]) {
Book newBook = new Book(1014, "Java and OOPs", "RS Aggarwal", 5);
System.out.println("The book before it is serialized: " + newBook);
//Leveraging try-catch methods
try {
FileOutputStream stream1 = new FileOutputStream("newBook.ser");
ObjectOutputStream stream2 = new ObjectOutputStream(stream1);
stream2.writeObject(newBook);
stream1.close();
stream2.close();
// Depicts the message for successfully serialized book
System.out.println("Book is successfully Serialized ");
FileInputStream inputStream1 = new FileInputStream("newBook.ser");
ObjectInputStream inputStream2 = new ObjectInputStream(inputStream1);
Book oldBook = (Book) inputStream2.readObject();
inputStream1.close();
inputStream2.close();
//Displays the successful messages
System.out.println("The book has been created from serialized information");
System.out.println("The book just after the seriazliation is : " + oldBook); )
} catch (Exception e) {
e.printStackTrace();
}
}
}
// create class Book that implements the Serializable interface and has a transient variable.
class Book implements Serializable{
//Variable declaration is supposed to be done here
privateint ISBN; // every book has a unique ISBN number
private String title; // every book has title
private String author; //every book has been written by an author
private transient int edition = 1; // edition of the book, i.e., transient variable not serialized
//a variable is created with help of a constructor
public Book(intisbn, String ttl, String auth, int edition) {
this.ISBN = isbn;
this.title = ttl;
this.author = auth;
this.edition = edition;
}
public String toString() {
return "Book" + " ISBN= " + ISBN + ", title=" + title + ", author=" + author + ", edition=" + edition + '}';
}
}
Output
The book before it is serialized: Book ISBN= 1014, title= Java and OOPs, author= RS Aggarwal, edition=5
The book has been created from serialized information
The book just after the serialization is : Book ISNB=1014, title= Java and OOPs, author = RS Aggarwal, edition= 0
The use of temporary keywords is crucial for observing security restrictions. There are many situations in real life where we don't want to store private information in a file. The transitory keyword can also be used to avoid serializing variables whose values can be estimated 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 good practise.
Using the transient keyword alongside static variables is useless because static fields aren't a part of the object's state. There's no compilation error, though.
Declaring a final variable to be temporary serves no purpose because final variables are explicitly serialized by their values. However, there isn't a compile-time error.