Is constructor of class called during DeSerialization process in java



It depends on whether our object has implemented Serializable or Externalizable.

If Serializable has been implemented - constructor is not called during DeSerialization process.

But, if Externalizable has been implemented - constructor is called during DeSerialization process.



Full Program/SourceCode to show that If Serializable has been implemented - constructor is not called during DeSerialization process.
package SerDeser7SerConsCheck;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */

/*Author : AnkitMittal  Copyright- contents must not be reproduced in any form*/
class Employee implements Serializable {
  
   private static final long serialVersionUID = 1L;
   private Integer id;
  
   public Employee(){
          System.out.println("No-arg constructor called");
   }
  
   public Employee(Integer id) {
          System.out.println("1-arg constructor called");
          this.id = id;
   }
   @Override
   public String toString() {
          return "Employee [id=" + id + "]";
   }
}
public class SerializeConstructorCheck {
   public static void main(String[] args) {
          Employee object1 = new Employee(8);
          try {
                 OutputStream fout = new FileOutputStream("ser.txt");
                 ObjectOutput oout = new ObjectOutputStream(fout);
                 System.out.println("Serialization process has started, serializing employee objects...");
                 oout.writeObject(object1);
                 fout.close();
        oout.close();
        System.out.println("Object Serialization completed.");
                
                 //DeSerialization process >
                
                
                 InputStream fin=new FileInputStream("ser.txt");
                 ObjectInput oin=new ObjectInputStream(fin);
                 System.out.println("\nDeSerialization process has started, displaying employee objects...");
                 Employee emp=(Employee)oin.readObject();
                 System.out.println(emp);
                 fin.close();
       oin.close();
       System.out.println("Object DeSerialization completed.");
                
                
          } catch (IOException | ClassNotFoundException  e) {
                 e.printStackTrace();
          }
   }
}
/*OUTPUT
1-arg constructor called
Serialization process has started, serializing employee objects...
Object Serialization completed.
DeSerialization process has started, displaying employee objects...
Employee [id=8]
Object DeSerialization completed.
*/
If, we note output, constructor is not called during DeSerialization process.


Full Program/SourceCode to show that if Externalizable has been implemented - constructor is called during DeSerialization process.
>
package SerDeser7ExtConsCheck;
import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
/*Author : AnkitMittal  Copyright- contents must not be reproduced in any form*/
class Employee implements Externalizable  {
  
   private static final long serialVersionUID = 1L;
   private Integer id;
  
   public Employee(){
          System.out.println("No-arg constructor called");
   }
  
   public Employee(Integer id) {
          System.out.println("1-arg constructor called");
          this.id = id;
   }
   @Override
   public String toString() {
          return "Employee [id=" + id + "]";
   }
   /*
   * define how Serialization process will write objects.  
   */
   @Override
   public void writeExternal(ObjectOutput oo) throws IOException {
          oo.writeInt(id);
   }
  
   /*
   * define how deSerialization process will read objects.  
   */
   @Override
   public void readExternal(ObjectInput in) throws IOException,  ClassNotFoundException {
          this.id=in.readInt();
   }
}
public class ExternalizableConstructorCheck {
   public static void main(String[] args) {
          Employee object1 = new Employee(8);
          try {
                 OutputStream fout = new FileOutputStream("ser.txt");
                 ObjectOutput oout = new ObjectOutputStream(fout);
                 System.out.println("Serialization process has started, serializing employee objects...");
                 oout.writeObject(object1);
                 fout.close();
           oout.close();
                 System.out.println("Object Serialization completed.");
                
                 //DeSerialization process >
                
                
                 InputStream fin=new FileInputStream("ser.txt");
                 ObjectInput oin=new ObjectInputStream(fin);
                 System.out.println("\nDeSerialization process has started, displaying employee objects...");
                 Employee emp=(Employee)oin.readObject();
                 System.out.println(emp);
                 fin.close();
       oin.close();
       System.out.println("Object DeSerialization completed.");
                
                
          } catch (IOException | ClassNotFoundException  e) {
                 e.printStackTrace();
          }
   }
}
/*OUTPUT
1-arg constructor called
Serialization process has started, serializing employee objects...
Object Serialization completed.
DeSerialization process has started, displaying employee objects...
No-arg constructor called
Employee [id=8]
Object DeSerialization completed.
*/
If, we note output, constructor is called during DeSerialization process.




RELATED LINKS>


serialVersionUID  >

Constructor call during DeSerialization >


No comments:

Post a Comment