Will default constructor is run When we deserialized of serialized object that implements serializable interface ?

Concept : Answer will NO, when we deserialized the object that implements serializable interface Default constructor will not run
Code :
 class SerializationBox  implements Serializable { 
  public int a=900;
  private int serializableProp = 100;
  private transient int b=800;
  private int c;
  private static String st;
  private  String st1;
  public SerializationBox()
  {
   System.out.println("The Default constructor running");
  }
  public SerializationBox(int a)
   {
 this.c=a;
                   System.out.println("The constructor running");
   }
  public int getSerializableProp() { 

    return serializableProp; 
  } 
  public int getStaticValue() { 

     return b; 
   } 

public class SerializationSample { 
public static void main(String args[]) throws IOException,  FileNotFoundException, ClassNotFoundException
  { 
    SerializationBox serialB = new SerializationBox(10); 
    serialize("C:\\Temp\\kumud.txt", serialB); 
    System.out.println("After Serialize");
    SerializationBox sb = (SerializationBox) deSerialize("C:\\Temp\\kumud.txt"); 
    System.out.println(sb.getSerializableProp()); 
    System.out.println("Static Value"+sb.getStaticValue()); 

  } 
  
  public static void serialize(String outFile, Object serializableObject)  throws IOException { 
    FileOutputStream fos = new FileOutputStream(outFile); 
    ObjectOutputStream oos = new ObjectOutputStream(fos); 
    oos.writeObject(serializableObject); 

  } 
  
  public static Object deSerialize(String serilizedObject)  throws FileNotFoundException, IOException, ClassNotFoundException { 
    FileInputStream fis = new FileInputStream(serilizedObject); 
    ObjectInputStream ois = new ObjectInputStream(fis); 
    return ois.readObject(); 

  } 
}
Output :
The constructor running
After Serialize
100
Static Value0
Concept :
In above output  when object desrialized default constructor will not run

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.