Are the constructors in an object invoked when de-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.

A constructor is similar to method and it is invoked at the time creating an object of the class, it is generally used to initialize the instance variables of a class. The constructors have same name as their class and, have no return type.
If you do not provide a constructor the compiler defines one on your behalf, which initializes the instance variables with default values.

Constructors and deserialization

When we de-serialize an object, the constructor of its class is never called. Consider the following example, here we have a class named student with two instance variables and a default constructor (initializing with two hardcoded values) and a parameterized constructor.

The display() method of this class displays the variable values of the current instance.
We are creating an object of the Student class by passing two values (vani and 27) and serializing it.
When we de-serialize this object and invoke the display() method the values we passed will be printed. and the static variables print the new (current) value from the class.

Example

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

Updated on: 02-Jul-2020

765 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements