The knowledge sharing zone - Guruzon.com

Use of transient keyword

This tutorial explains the use of transient keyword with various different aspects. Transient variables are very useful in serialization scenarios. You can have transient variables by using transient keyword in Java.
reviews

What is transient variable - Use of transient variable

  • 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.
  • Serialization is one of the coolest features of Java, it lets you save (sometimes called "flatten") an object by writing its state (the value of its instance variables) to a special type of I/O stream.
  • With serialization you can save an object to a file, or even ship it over a wire for reinflating (deserializing) at the other end, in another JVM.
  • When you mark a variable as transient, which means - "Don't include the transient variable's value as part of the object's serialized state".
  • For example, When an instance variable is declared as transient, then its value need not persist when an object is stored.
class T implements Serializable { 
    transient int a; // will not persist 
    int b; // will persist
}

What is Serializable? - Use of transient variable with Serialization

  • Serializable is a marker interface in java.io package.
  • Serialization is the process of writing the state of an object to a byte stream. This is useful when you want to save the state of your program to a persistent storage area, such as a file. At a later time, you may restore these objects by using the process of deserialization.
  • Serializable instructs JVM that objects belong to Serializable class can be streamed / saved to persisted to storage / transfer over network.
  • Remember that Only objects of Classes that implements the Serializable interface can be saved and restored by the serialization facilities.
  • The Serializable interface defines no members. It is simply used to indicate that a class may be serialized. 
  • If a class is serializable, all of its subclasses are also serializable.
  • Variables that are declared as transient are not saved by the serialization facilities. 
  • Also, static variables are not saved.

Example of Serialization and Deserialization with Transient and Static fields

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 SerializationDemo {
    public static void main(String args[]) {
        // Object serialization
        try {
            MyClass object1 = new MyClass("Hello", -7, 2.7e10);
            System.out.println("object1: " + object1);
            FileOutputStream fos = new FileOutputStream("serial");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(object1);
            oos.flush();
            oos.close();
        } catch (IOException e) {
            System.out.println("Exception during serialization: " + e);
            System.exit(0);
        }

        // Object deserialization
        try {
            MyClass object2;
            FileInputStream fis = new FileInputStream("serial");
            ObjectInputStream ois = new ObjectInputStream(fis);
            object2 = (MyClass) ois.readObject();
            ois.close();
            System.out.println("object2: " + object2);
        } catch (Exception e) {
            System.out.println("Exception during deserialization: " + e);
            System.exit(0);
        }
    }
}

class MyClass implements Serializable {

    private static final long serialVersionUID = 1L;

    // will be persisted

    String s;
    int i;
    double d;

    // will not be persisted
    static String staticString;
    transient int transientInt;

    public MyClass(String s, int i, double d) {

        this.s = s;
        this.i = i;
        this.d = d;
    }

    public String toString() {
        return "s=" + s + "; i=" + i + "; d=" + d + "; staticString="
                + staticString + "; transientInt=" + transientInt;
    }
}

Output:
    object1: s=Hello; i=-7; d=2.7E10; staticString=null; transientInt=0
    object2: s=Hello; i=-7; d=2.7E10; staticString=null; transientInt=0

 

You would also like to read:

Use of strictfp keyword
Use of volatile keyword
Use of final keyword
Use of static keyword
Use of native keyword
Use of synchronized keyword
Use of abstract keyword

Comments :

Name :
Email :
Comment :
Verify Text :
capch image, refresh page if not loaded somehow