title: How Deserialization works?
body: <p>As far as my understanding goes constructor of class whose Object is serialized is not called but the no-arg constructor of 1st non serializable constructor. Now consider following code</p>
  
  <pre><code>public class SerializeDemo implements Serializable {
  
  private String name;
  int age;    //default 0
  
  public SerializeDemo(String name, boolean setAge){
      this.name = name;
      if(setAge){
          this.age = 18;
      }
  }
  
  @Override
  public String toString() {
      return "Name is " + name + " and age is " + age;
  }
  
  public static void main(String args[]) throws IOException, ClassNotFoundException {
  
      ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("//home//aniket//Desktop//serializedObjects.txt")));
      ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("//home//aniket//Desktop//serializedObjects.txt")));
      SerializeDemo sd = new SerializeDemo("Test",true);
      System.out.println("Before Serialization : " + sd);
      oos.writeObject(sd);
      SerializeDemo sdCopy = (SerializeDemo)ois.readObject();
      System.out.println("After Deserialization : " + sdCopy);
  }
  }
  </code></pre>
  
  <p>and the output is(as expected)</p>
  
  <pre><code>Before Serialization : Name is Test and age is 18
  After Deserialization : Name is Test and age is 18
  </code></pre>
  
  <p>Now the the non serializable super class having no-arg constructor is Object(Correct me if I am wrong). So basically SerializeDemo constructor is not called.</p>
  
  <p>Now when the Object is created during deserialization it will try to rebuild the instance state. So it will set age to 18. </p>
  
  <p>Question is how?</p>
  
  <p>I have deliberately not provided the setter. Nor as per above discussion it's constructor is called. So how is it set?(Same goes for name as well)</p>