Advertisement



< Prev
Next >



Java - Transient Keyword






What is "transient" in Java?


In Java, transient is a keyword. When an object is being serialized, its instance variables which are declared with the transient keyword are skipped by the process of serialization i.e. when an object is serialized by the process of serialization, the state of transient instance variables of this object is not saved.




Why "transient" keyword is used?


In some situations when an object is serialized, we may not want to save the state of a few unimportant instance variables of an object. In this case, we only need to declare these instance variables with the transient keyword.




What happens to an instance variable with "transient" keyword in Serialization?


After an object's state is saved by the process of serialization, when this saved object is deserialized, its instance variables marked with the transient keyword are assigned their default values. Let us take a look at the different instance variable types and their default values.

  • Instance variable type
  • Default value
  • boolean
  • false
  • byte, short, int, long
  • zero
  • float,double
  • 0.0
  • character
  • '\u0000'
  • Object reference variable
  • null

    Such default values are implicitly given to the transient declared instance variables of an object during deserialization.

    Advertisement




    Let's understand "transient" with an example -:


    //Java - Example of transient keyword 
    
    
    import java.io.*;
    
    //A class implementing Serializable interface 
    class TransientExm implements Serializable	
    {
    transient int i=10;	//instance variable of primitive int type is marked "transient"
    public static void main(String... ar)
    {
    TransientExm ob= new TransientExm();
    System.out.println("Before Serialization object has value : " + ob.i);
    try
    {
    	//Writing saved state of the object in this file
    	FileOutputStream fos= new FileOutputStream("E:\\File.ser");
    	ObjectOutputStream oos= new ObjectOutputStream(fos);
    	
    	//Serializing, i.e. saving the state of this object, ob
    	oos.writeObject(ob);	
    	oos.close();
    }
    catch(Exception exp)
    {
    	System.out.println(exp);
    }
    
    try
    {
    	//Reading the same file for Deserialization
    	FileInputStream fis= new FileInputStream("E:\\File.ser");	
    	ObjectInputStream ois= new ObjectInputStream(fis);
    	
    	//Deserializing, i.e. restoring the object to its original state.
    	ob=(TransientExm)ois.readObject();	
    	ois.close();
    	System.out.println("After Deserialization restored object has value : "+ ob.i);
    }
    catch(Exception exp)
    {
    	System.out.println(exp);
    }
    
    
    }
    
    }
    
    


    Output is -:


    Before Serialization object has value : 10
    After Deserialization restored object has value : 0


    Program Analysis





    Please share this article -




    < Prev
    Next >
    < What is Serialization?
    Static in Serialization >



    Advertisement

    Please Subscribe

    Please subscribe to our social media channels for daily updates.


    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




    Advertisement



    Notifications



    Please check our latest addition

    C#, PYTHON and DJANGO


    Advertisement