Java Transient
Java Transient
In Java, Serialization is used to convert an object into a stream of the byte. The byte stream consists of the data of the instance as well as the type of data stored in that instance. Deserialization performs exactly opposite operation. It converts the byte sequence into original object data.During the serialization, when we do not want an object to be serialized, we can use a transient keyword.
The following program shows how serialization is performed.
SampleSerialize.java
import java.io.*; /* serializable class */ class Employee implements java.io.Serializable { public String empname; public String empaddress; public int emppassword; public int empid; public void mailCheck() { System.out.println("Check a mail to " + empname + " " + empaddress); } } public class SampleSerialize { /* Driver Code */ public static void main(String [] args) { Employee e = new Employee(); e.empname = "ABC"; e.empaddress = "XYZ"; e.emppassword = 11122333; e.empid = 401; try { FileOutputStreamfileOut = new FileOutputStream("empinfo.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(e); out.close(); fileOut.close(); System.out.printf("Serialized data is saved in empinfo.ser"); } catch (IOExceptioni) { i.printStackTrace(); } } }
Output:
Serialized data is saved in empinfo.ser
In the above code, the Employee class is serialized and the serialized objects are stored inside the file named empinfo.ser. Here, we do not want to serialize the data member emppassword.
Further in this article, we will be discussing how transient keyword can be used to avoid the serialization of a data member.
Why use the transient keyword?
The transient keyword can be used with the data members of a class in order to avoid their serialization. For example, if a program accepts a user’s login details and password. But we don’t want to store the original password in the file. Here, we can use transient keyword and when JVM reads the transient keyword it ignores the original value of the object and instead stores the default value of the object.
Syntax:
privatetransient<
member variable>;
Or
transient private<member variable>;
When to use the transient keyword?
- The transient modifier can be used where there are data members derived from the other data members within the same instance of the class.
- This transient keyword can be used with the data members which do not depict the state of the object.
- The data members of a non-serialized object or class can use a transient modifier.
The following program demonstrates the use of transient keyword.
SampleSerialize.java
import java.io.*; /* serializable class */ class Employee implements java.io.Serializable { public String empname; public String empaddress; public transient int emppassword; public int empid; public void mailCheck() { System.out.println(“Check a mail to " + empname + " " + empaddress); } } public class SampleSerialize { /* Driver Code */ public static void main(String [] args) { Employee e = new Employee(); e.empname = "ABC"; e.empaddress = "XYZ"; e.emppassword = 11122333; e.empid = 401; try { FileOutputStreamfileOut = new FileOutputStream("empinfo.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(e); out.close(); fileOut.close(); System.out.printf("Serialized data is saved in empinfo.ser"); } catch (IOExceptioni) { i.printStackTrace(); } } }
Output:
Serialized data is saved in empinfo.ser
In the above code, the Employee class is serialized and the serialized objects are stored inside the file named empinfo.ser. Here, we do not want to serialize the data member emppassword. So, to achieve that we have used transient keyword before the data member name. Now if we check the contents of the file empinfo.ser it will have the default value for the transient variable.
Use of transient with final keyword
Consider the Employee class from above example. If we modify the data members of Employee class they will behave differently.
class Employee implements Serializable { public int empid; public String empname; public String empaddress; public transient int emppassword = "123456"; public final transient Lock lock = Lock.getLock("demo"); }
Output:
401 ABC XYZ 123456 null
Here, all the other data members are returning the values as they are initialized. Even though we have used thetransient property for the emppassword variable, it is behaving like the other data members. This is because when a final data member is initialized the JVM reads it as a constant expression and performs serialization operation on it. But in the similar case of thelock, it is not serialized because it is a reference.
In this article, we have discussed the concept of serialization in Java, use of transient keyword, where to use transient keyword and how it behaves differently when used with the final keyword.