Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Transient Java Keyword

In this section we will discuss about transient keyword in java. Transient is a keyword in java which is used to prevent any variable being serialized

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads

Transient Java Keyword

In this section we will discuss about transient keyword in java. Transient is a keyword in java which is used to prevent any variable being serialized. Before going into details about transient we will try to learn about serialization in java. Serialization is used to save the state of object by JVM and during deserialization it is recovered. Serialization save the state of object but if we don't want to make the property of an object serialized we can do it by using transient.

Why do we need transient?

Transient keyword provide you a control on serialization process in java and allow to exclude some of the object properties which you don't want to in serialization process.

Which variable should mark as transient?

By making a variable transient it will not involve in serialization process hence the variable whose value can be calculated from another variables doesn't require to be saved should be marked as transient.

Some of the important facts about transient are as follows :

  • Transient is used with variable or field only.
  • Transient variable in java is not saved because it is not serialized.
  • You can make a variable both transient and static but doing so is not meaningful. Transient does not allow you to serialize and static is not saved during serialization.

Syntax :

 transient data-type variable-name;

Example :

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
 
class Demo1 implements Serializable{
    private String Name;
    private transient String address;  //making address transient is not serialized
   
   public Demo1(){}
    public Demo1 (String Name,String address)
    {
        this.Name = Name;
        this.address=address;
        
    }
   public void display()
   {
	   System.out.println("Name = "+Name);
       System.out.println("Address = "+address);
   }
}
public class MainDemo
  {
    public static void main(String args[]) throws Exception 
    {
        Demo1 ob = new Demo1("Roseindia","Rohini");
        ObjectOutputStream o = new ObjectOutputStream
                   (new FileOutputStream("Demo"));
        // writing to object
        o.writeObject(ob);
        o.close();
       ObjectInputStream in=new ObjectInputStream(new FileInputStream("Demo"));
        Demo1 ob1=(Demo1)in.readObject();
        //System.out.println(ob1);
        ob1.display();
        
    }
  }

Output :  After compiling and executing the program.

Download Sourcecode

Advertisements

Ads
Share on Google+Share on Google+

Transient Java Keyword

Posted on: June 3, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Transient Java Keyword  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads