Wednesday, 27 June 2012

Transient variable in java

Serialization is the process of saving an object state in a storage medium (such as a file, or a memory buffer) or to transmit it over a network connection in binary form.The object can be restored (Deserialization) at a later time, and even a later location. With persistence, we can move an object from one computer to another, and have it maintain its state.

If you mark an instance variable as transient, you're telling the JVM to skip (ignore) this variable when you attempt to serialize the object containing it. If you have variables marked transient, they will not be restored to their original state (unless you implement readObject()), but will instead be given

the default value for that data type.In other words, even if you say

class User implements Serializable {
transient int password = 12345;
}

when the User instance is deserialized, the variable password will be set to a value of 0.Object references marked transient will always be reset to null, regardless of whether they were initialized at the time of declaration in the class.

class Employee implements Serializable{
transient java.util.Date joinDate=new java.util.Date();
}

when the Employee instance is deserialized, the object reference joinDate will be set to a value of  null.

Let us see one example:

A password member variable might not be safe to transmit to third parties over a network connection, and might need to be left blank. In this case, the transient keyword can be used.


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class TransientVariable{

 public static void main(String args[]) throws ClassNotFoundException {

  ObjectInputStream in = null;
  ObjectOutputStream out = null;
  try {
   out = new ObjectOutputStream(new FileOutputStream("serial.out"));
   in = new ObjectInputStream(new FileInputStream("serial.out"));

   /** Create one User object to serialize **/
   User user1 = new User("root", 12345);

   /** Serialize the object user1 **/
   out.writeObject(user1);

   /** Deserialize the object **/
   User user1_copy = (User) in.readObject();
   System.out.println(user1_copy);

  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   try {
    out.close();
    in.close();
   } catch (IOException e) {
    e.printStackTrace();
   }

  }
 }

}

class User implements Serializable {
 /**
  * serialVersionUID is used for version control of serialized object.
  **/
 private static final long serialVersionUID = -8313505998489596676L;

 private String username;

 /** instance variable password is declared as transient **/
 transient private long password;

 public User(String username, long password) {
  this.username = username;
  this.password = password;
 }

 public String toString() {
  return ("Username =" + username + " & Password =" + password);
 }
}


The output is

Username =root & Password =0

You can see that the transient variable password is not restored to its original state( i.e. 12345), instead given the default value of the int data type 0.

No comments:

Post a Comment