Thursday, 12 January 2017

Why does Java have transient fields?

The transient variables are never serialized during the Serialization process   and initialize with default value during Deserialization process.

The below example simplify the transient keyword uses. In this example we are consider the id field in Employee is transient variable. In this below example before Serialization Employee object value [id=100, name=Sitansu, Address=BBSR] and After Serialization the Employee object   [id=0, name=Sitansu, Address=BBSR]. This example shows the id field after Serialization assign to default value to 0.


package Java91.blogspot.in;

import java.io.Serializable;

public class Employee implements Serializable{
      
       private static final long serialVersionUID = 1L;
       private transient int id=100;
       private String name;
       private String Address;
      
       public Employee(int id,String name,String Address){
              this.id=id;
              this.name=name;
              this.Address=Address;
       }

       public int getId() {
              return id;
       }

       public void setId(int id) {
              this.id = id;
       }

       public String getName() {
              return name;
       }

       public void setName(String name) {
              this.name = name;
       }

       public String getAddress() {
              return Address;
       }

       public void setAddress(String Address) {
              this.Address = Address;
       }

       @Override
       public String toString() {
              return "Employee [id=" + id + ", name=" + name + ", Address=" + Address + "]";
       }

}

package Java91.blogspot.in;

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

public class DemoTransientTest {
         public static void main(String args[]) {
               
                Employee narnia = new Employee(100,"Sitansu","BBSR");
              System.out.println("Before Serialization: " + narnia);
            
               try {
                   FileOutputStream fos = new FileOutputStream("narnia.ser");
                   ObjectOutputStream oos = new ObjectOutputStream(fos);
                   oos.writeObject(narnia);

                   System.out.println("Employee details is successfully Serialized ");

                   FileInputStream fis = new FileInputStream("narnia.ser");
                   ObjectInputStream ois = new ObjectInputStream(fis);
                   Employee oldNarnia = (Employee) ois.readObject();
                
                   System.out.println("Employee details successfully created from Serialized data");
                   System.out.println("Employee details after seriazliation : " + oldNarnia);
                
               } catch (Exception e) {
                   e.printStackTrace();
               }

           }

}


Output:
Before Serialization: Employee [id=100, name=Sitansu, Address=BBSR]
Employee details is successfully Serialized
Employee details successfully created from Serialized data

Employee details after seriazliation : Employee [id=0, name=Sitansu, Address=BBSR]

1 comment:

  1. Good note on transient field. You can read about Serialization process on http://www.gauravbytes.com/2014/09/serialization-in-java-with-example.html

    ReplyDelete