...
Code Block |
---|
public class Person { private String name; Person() { // do nothing - needed for serialization } Person(String theName) { name = theName; } public String getName () { return name; } // other details not relevant to this example } public class Student extends Person implements Serializable { private Professor tutor; Student() { // do nothing - needed for serialization } Student(String theName, Professor theTutor) { super(theName); tutor = theTutor; } public Professor getTutor() { return tutor; } } public class Professor extends Person implements Serializable { private ArrayList<Student>List<Student> tutees = new ArrayList<Student>(); Professor() { // do nothing - needed for serialization } Professor(String theName) { super(theName); } public ArrayList<Student>List<Student> getTutees () { return tutees; } /** * checkTutees checks that all the tutees * have this Professor as their tutor */ public boolean checkTutees () { boolean result = true; for (Student stu: tutees) { if (stu.getTutor() != this) { result = false; break; } } return result; } } // ... Professor jane = new Professor("Jane"); Student able = new Student("Able", jane); Student baker = new Student("Baker", jane); Student charlie = new Student("Charlie", jane); jane.getTutees().add(able); jane.getTutees().add(baker); jane.getTutees().add(charlie); System.out.println("checkTutees returns: " + jane.checkTutees()); // prints "checkTutees returns: true" |
...
Code Block | ||
---|---|---|
| ||
String filename = "serial"; trytry { // Serializing using writeUnshared ObjectOutputStream ObjectOutputStream oos = new ObjectOutputStream = new ObjectOutputStream(new FileOutputStream(filename)); oos.writeUnshared(jane); oos.close(); // Deserializing using readUnshared ObjectInputStream ois = new ObjectInputStream = new ObjectInputStream(new FileInputStream(filename)); Professor jane2 = (Professor)ois.readUnshared(); ois.close(); System.out.println("checkTutees returns: " + System.out.println("checkTutees returns: " + jane2.checkTutees()); // prints "checkTutees returns: false" } catch (Exception e) { System.out.println("Exception during deserialization" + e); } |
...
Code Block | ||
---|---|---|
| ||
String filename = "serial"; try { System.out.println("// Serializing using writeObject");writeUnshared ObjectOutputStream oos = new ObjectOutputStream = new ObjectOutputStream(new FileOutputStream(filename)); oos.writeObject(jane); oos.close(); System.out.println("// Deserializing using readObject");readUnshared ObjectInputStream ois = new ObjectInputStream = new ObjectInputStream(new FileInputStream(filename)); Professor jane2 = (Professor)ois.readObject(); ois.close(); System.out.println("checkTutees returns: " + System.out.println("checkTutees returns: " + jane2.checkTutees()); // prints "checkTutees returns: true" false" } catch (Exception e) { System.out.println("Exception during deserialization" + e); } |
Applicability
Using the writeUnshared()
and readUnshared()
methods may produce unexpected results.Automated detection is straightforward.
Bibliography
[API 2011] | Classes ObjectOutputStream and ObjectInputStream |
...