I am trying to write a instance of pojo class by using WriteObject method. when i write code like this :
private void writeObject(ObjectOutputStream oos) throws IOException,ClassNotFoundException{
oos.defaultWriteObject();
oos.writeObject(this);
}
It works fine but when I try and create a new local object and pass it to writeObject method it fails with
Exception in thread "main" java.lang.StackOverflowError
can some one please explain why it keeps calling writeObject method again and again recursively?
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;
}
}