title: Custom Serialization using ReadObject and WriteObject
body: <p>I am trying to write a instance of pojo class by using WriteObject method. when i write code like this :</p>
  
  <pre><code>    private void writeObject(ObjectOutputStream oos) throws IOException,ClassNotFoundException{ 
      oos.defaultWriteObject();    
      oos.writeObject(this);
  }
  </code></pre>
  
  <p>It works fine but when I try and create a new local object and pass it to writeObject method it fails with </p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.StackOverflowError</p>
  </blockquote>
  
  <p>can some one please explain why it keeps calling writeObject method again and again recursively?</p>
  
  <pre><code>class Employee implements Serializable{
      private String name;
      private int age;
      private void readObject(ObjectInputStream ois) throws IOException,ClassNotFoundException{
          ois.defaultReadObject();
          Employee emp = (Employee)ois.readObject();
          emp.toString();
      }
      private void writeObject(ObjectOutputStream oos) throws IOException,ClassNotFoundException{
          oos.defaultWriteObject();
          Employee emp = new Employee("sumit",10);
          oos.writeObject(emp);
      }
      public Employee(){
  
      }
      public Employee(String name, int age){
          this.name = name;
          this.age = age;
      }   
  }
  </code></pre>