Java Reference
In-Depth Information
String filename = "serial";
try (ObjectOutputStream oos = new ObjectOutputStream(new
FileOutputStream(filename))) {
// Serializing using writeUnshared
oos.writeObject(jane);
} catch (Throwable e) {
// Handle error
}
// Deserializing using readUnshared
try (ObjectInputStream ois = new ObjectInputStream(new
FileInputStream(filename))) {
Professor jane2 = (Professor)ois.readObject();
System.out.println("checkTutees returns: " +
jane2.checkTutees());
} catch (Throwable e) {
// Handle error
}
Applicability
Using the writeUnshared() and readUnshared() methods may produce unexpected
results when used for the round-trip serialization of data structures containing reference
cycles.
Bibliography
[API 2013]
Class ObjectOutputStream
Class ObjectInputStream
75. Do not attempt to help the garbage collector by setting local reference
variables to null
Setting local reference variables to null to “help the garbage collector” is unnecessary. It
adds clutter to the code and can make maintenance difficult. Java just-in-time compilers
(JITs) can perform an equivalent liveness analysis and most implementations do so.
A related bad practice is the use of a finalizer to null out references. See The CERT ®
Oracle ® Secure Coding Standard for Java [Long 2012], “MET12-J. Do not use final-
izers,” for additional details.
Search WWH ::




Custom Search