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.


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"); = "Krishna";
      this.age = 25;
   public Student(String name, int age){ = name;
      this.age = age;
   public void display() {
      System.out.println("Name: ";
      System.out.println("Age: "+this.age);
      System.out.println("Year: "+Student.year);
   public void setName(String 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);
      //Printing the data before de-serialization
      System.out.println("Values before de-serialization");
      //Changing the static variable value
      //Changing the instance variable value
      //Changing the transient variable value
      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.......");
      System.out.println("Values after de-serialization");


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