What is transienet keyword and why do you need it?

Technology: java
AskedIn:
Topics:
Type:
  • transient is a variable modifier used in serialization
  • it is used to make the JVM ignore variables' state during the process of serialization by marking them transient
  • JVM ignores the original value of transient variables and save the default value of that variable data type
  • it is also used with the fields which are not marked as “Serializable” inside JDK. Because certain classes which do not implement Serializable interface when they are referenced within any serializable class and cannot be serialized will throw NotSerializableException
  • a final variable will get serialized even after making it transient
  • transient variable is redundant for static variables because only instance variables gets serialized not those variables which belongs to class

Output

  • It helps in scenarios that if we don’t want to save any particular private/confidential data in file
  • If some variables' value can be derived/calculated using other serialized objects then it helps in not serializing those variables

Example

import java.io.*;

public class TransientDemo implements Serializable {
    String name;
    transient int num;
    transient final int num2 = 50;

    // transient keyword is redundant for static variable
    transient static int num3;

    TransientDemo(String name, int num, int num3) {
        this.name = name;
        this.num = num;
        this.num3 = num3;
    }

    @Override
    public String toString() {
        return "TransientDemo{" +
                "name='" + name + '\'' +
                ", transient num=" + num +
                ", transient final num2=" + num2 +
                ", transient static num3=" + num3 +

                '}';
    }

    public static void main(String[] args) {
        TransientDemo serializeObj = new TransientDemo("Cortana", 20, 30);
        String fileName = "transient_demo.txt";
        // Serializing Object to a file
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(fileName);
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
            System.out.println("Data before serialization");
            System.out.println(serializeObj);
            objectOutputStream.writeObject(serializeObj);
        } catch (FileNotFoundException e) {
            System.out.println("FileNotFoundException caught");
        } catch (IOException e) {
            System.out.println("IOException caught");
        }

        // Deserializing object from a file
        TransientDemo deserializeObj = null;
        try{
            FileInputStream fileInputStream = new FileInputStream(fileName);
            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
            deserializeObj = (TransientDemo) objectInputStream.readObject();
            System.out.println("data after deserialization");
            System.out.println(deserializeObj.toString());
        } catch (FileNotFoundException e){
            System.out.println("FileNotFoundException caught");
        } catch (IOException e){
            System.out.println("IOException caught");
        } catch (ClassNotFoundException e){
            System.out.println("ClassNotFoundException caught");
        }
    }
}
 

Output

Data before serialization

TransientDemo{name='Cortana', transient num=20, transient final num2=50, transient static num3=30}

data after deserialization

TransientDemo{name='Cortana', transient num=0, transient final num2=50, transient static num3=30}