Are the values of static variables stored when an object is serialized in Java?


In Java, serialization is a concept using which we can write the state of an object into a byte stream so that we can transfer it over the network (using technologies like JPA and RMI).

To serialize an object −

  • Make sure the class implements the Serializable interface.
  • Create a FileOutputStream object representing the file (abstract path) of the file to which the object is to be stored.
  • Create a ObjectOutputStream object by passing the above created FileOutputStream object.
  • Write the object to the file using the writeObject() method.

To de-serialize an object

  • Create a FileInputStream object representing the file that contains the serialized object.
  • Read the object from the file using the readObject() method.
  • Use the retrieved object.

Variables in a de-serialized object

When we de-serialized an object only, instance variables are saved and will have same values after the process.

Transient variables − The values of the transient variables are never considered (they are excluded from the serialization process). i.e. When we declare a variable transient, after de-serialization its value will always be null, false, or, zero (default value).

Static variables − The values of static variables will not be preserved during the de-serialization process. In-fact static variables are also not serialized but since these belongs to the class. After de-serialization they get their current values from the class.

Example

In this program we have changed the values of the instance, static and, transient variables of the class just before de-serializing it.

After the process the value of the instance variables will be same. The transient variables display the default values, and the static variables print the new (current) value from the class.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Student implements Serializable{
   private String name;
   private transient int age;
   private static int year = 2018;
   public Student(){
      System.out.println("This is a constructor");
      this.name = "Krishna";
      this.age = 25;
   }
   public Student(String name, int age){
      this.name = name;
      this.age = age;
   }
   public void display() {
      System.out.println("Name: "+this.name);
      System.out.println("Age: "+this.age);
      System.out.println("Year: "+Student.year);
   }
   public void setName(String name) {
      this.name = name;
   }
   public void setAge(int age) {
      this.age = age;
   }
   public void setYear(int year) {
      Student.year = year;
   }
}
public class SerializeExample{
   public static void main(String args[]) throws Exception{
      //Creating a Student object
      Student std = new Student("Vani", 27);
      //Serializing the object
      FileOutputStream fos = new FileOutputStream("e:\\student.ser");
      ObjectOutputStream oos = new ObjectOutputStream(fos);
      oos.writeObject(std);
      oos.close();
      fos.close();
      //Printing the data before de-serialization
      System.out.println("Values before de-serialization");
      std.display();
      //Changing the static variable value
      std.setYear(2019);
      //Changing the instance variable value
      std.setName("Varada");
      //Changing the transient variable value
      std.setAge(19);
      System.out.println("Object serialized.......");
      //De-serializing the object
      FileInputStream fis = new FileInputStream("e:\\student.ser");
      ObjectInputStream ois = new ObjectInputStream(fis);
      Student deSerializedStd = (Student) ois.readObject();
      System.out.println("Object de-serialized.......");
      ois.close();
      fis.close();
      System.out.println("Values after de-serialization");
      deSerializedStd.display();
   }
}

Output

Values before de-serialization:
Name: Vani
Age: 27
Year: 2018
Object serialized.......
Object de-serialized.......
Values after de-serialization:
Name: Vani
Age: 0
Year: 2019
raja
Published on 02-Aug-2019 17:28:39
Advertisements