i can't figure out what's wrong, but what i'm expecting from the last line System.out.println is to print the name Catty, but the string is empty. why?
Code:import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
import java.io.Serializable;
public class SerializeCat implements Serializable {
public static void main(String[] args) {
Cat cat1 = new Cat("Catty");
Cat cat2 = new Cat();
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("cat.ser"));
out.writeObject(cat1);
out.close();
} catch (Exception e) {}
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream("cat.ser"));
cat2 = (Cat) in.readObject();
in.close();
} catch (Exception e) {}
System.out.println("The name of cat2 is " + cat2.getName());
}
}
the class Cat.java
Code:public class Cat {
private String name;
public Cat(String name) {
this.name = name;
}
public Cat() {
this("");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

