Here we are going to discuss about Transient variable in java.
Topics Covered :
What is Transient Variable
How to declare Transient Variable
Need of Transient Variable
Some Important Point about transient Variable
Example that demonstrate transient variable
What is Transient Variable
Transient is a java keyword which has some significant meaning that field or variable should not be serialized.
To discuss more about Transient Lets first discuss briefly about Serialization.
Serialization: Object serialization is the process of saving an object’s state to a sequence of bytes and during deserializaiton the saved state can be recovered by JVM.
Thus, transient keyword is applied only to the member variables of a class and it is used to indicate that the member variable should not be serialized when the class containing that transient variable is needed to be serialized.
For more details about serialization click here .
How to declare Transient Variable
transient String authorName= “Shobhna”;
transient : modified variable authorName as transient.
type :type can be any of data type in java.
variableName : variable name is any valid identifier .
In the above statement we have declared the variable autherName as transient. Hense after after deserializing the value we get for this variable will be null.
1 2 3 4 5 |
package com.j2eereference.coreJava; public class SerialableClassExample{ private transient String authorName; } |
The value of the transient variable will be the default value of the data type.
In the above example we are getting as null because of the String data type. If we declare transient variable as int and tries to retrieve the value , it will return 0.
Need of Transient Variable
Transient variable is used to indicate Java virtual machine that it is not a part of the persistent state of the object.
During serialization process it gives you flexibility so that you can exclude some variables from the serialization process because in Serialization except static and transient variable all property of object gets saved.
Some Important points about transient variable
1) Transient keyword can only be applicable to member variable or field, it is not applicable for methods, class and local variables.
2) Transient variable indicates Java virtual machine that it is not a part of the persistent state of the object.
3) Java compiler does not give compilation error if we declare variable static and transient at same time.
4)we can declare final and transient at same time .But sometimes it will not work as transient variable would not be serialized, so during deserialization process you have to initialize the field manually, but if it is declared final, the compiler would complaint about it.
5)We can make those variable as transient whose value can be determined from other variables .
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package com.j2eereference.coreJava; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class TransientExample implements Serializable{ private int principalamt; private int rate; private int time; private String name; transient private int interest; TransientExample(int p,int r,int t,String name) { this.name = name; interest=p*r*t; } public static void main(String [] args) { TransientExample t = new TransientExample(1000,5,1,"shobhna"); System.out.println("Before serialization: - " + t.name + " "+ t.interest); try { FileOutputStream fso = new FileOutputStream("textfile.txt"); ObjectOutputStream oso = new ObjectOutputStream(fso); oso.writeObject(t); oso.close(); } catch (Exception e) { e.printStackTrace(); } try { FileInputStream fsi = new FileInputStream("textfile.ser"); ObjectInputStream osi = new ObjectInputStream(fsi); t = (TransientExample)osi.readObject(); osi.close(); } catch (Exception e) { e.printStackTrace(); } System.out.println("After de-serialization:- " + t.name + " "+ t.interest); } } |
Output
Before serialization: – shobhna 5000
After de-serialization:- shobhna 0
Here you can notice that after deserialization we are not getting the interest calculated as we have declared the variable as transient.
Hemant says
July 18, 2012 at 2:46 pmThanks, It’s very simple and valuable explanation.