To know about Java transient, first you must understand Serialization because the roots of transient lies in Serialization or transient is used only with Serialization and in other places it is never used at all. By the by, transient is a keyword of Java used as access modifier (like static, final etc). It is used with instance variables only; that is, a class cannot be transient or a method or a local variable cannot be transient.
First let us understand Serialization.
1. What is Serialization?
If an object is to be written to a file or sent across network, the object should be serialized, else, the JVM throws "NotSerializableException". That is, JVM will not allow writing an object to a file unless it is serialized.
2. Why an object should be written to a file?
I get you the concept with small snippets of code. Observe,
Employee emp = new Employee();
emp.name = "S N Rao";
emp.designation = "Java Trainer";
emp.salary = 9999;
The properties of Employee emp are to be stored in a file, say. Why should we store? Storing in a data structure gives temporary storage where the values are available as long as program executes (as they are stored in RAM). But to have a permanent storage, we write the values to hard disk either in a file or in a database. When written to hard disk, the values can be retrieved later at any time.
3. Which I/O stream class is used to store the object in a file?
ObjectOutputStream is used to write an object with method writeObject(Object obj) and to read later ObjectInputStream is used with method readObject().
4. What Serialization does?
Serialization preserves the state of the object. That is, the values like name, designation and salary attached to emp object are never changed (or precision lost) after reading from the file later. This technically called as "preserving the state". That is, Serialization is used to preserve the state of the object.
5. Then, where Java transient variable comes into picture?
Sometimes, I may have some variables whose state is not required to preserve. That is, the variables are not required to be serialized. Then, declare them simply as transient.
int basicSalary; // it is serialized
int houseRent; // it is serialized
String name; // it is serialized
transient int totalSalary; // it is NOT serialized
I use totalSalary just for printing purpose and totalSalary variable is calculated on basicSalary, houseRent, PF, housingLoan etc., which may change every month. So totalSalary is derived from other fields (it is only a calculation part just required for pay-slip preparation). This type of variables, a Programmer, would NOT like to serialize. He simply declares them as transient.
Points at a glance
Following points are to be remembered while using Java transient.
- transient keyword can be applied to instance variables only and not with classes and local variables (if applied, compiler raises error).
- Another point to remember is a static variable also will not be serialized, but you will loose encapsulation. Declaring a variable both static and transient does not raise compilation error, but no meaning in that.
- A variable cannot be both final and transient (compiler does not show error) but there MAY be problem at deserialization (reinitializing the variable when read). Of course, it is not showing any error on my JDK 1.7, but try on your version.
A full Serialization program with Java transient keyword explained with screenshot is available at Java Serialization Example.