The documentation for ObjectOutputStream says what happens:
The default serialization mechanism for an object records the class of the object, the class signature, and the values โโof all non-transient and non-static fields. References to other objects (with the exception of temporary or static fields) also force these objects to be written. Several links to one object are encoded using the link exchange mechanism, so that the graphics of objects can be restored in the same form as when writing the original.
(My emphasis)
For example, if you have several links to one object, when the chart is restored, you will get several links to one restored version of this object, and not links to several equivalent instances.
Of course, if the serializable container implements a different mechanism, the behavior is dictated by this mechanism, not the standard one.
So, for example, if we have Thing and Test :
Thing.java :
import java.io.*; import java.util.*; public class Thing implements Serializable { private Map<String,String> map1; private Map<String,String> map2; public Thing() { this.map1 = new HashMap(); this.map2 = this.map1;
Test.java :
import java.io.*; public class Test implements Serializable { public static final void main(String[] args) { try {
And run java Test , we get:
t2.mapsAreSameObject? true
... because both members of Thing , map1 and map2 ultimately point to the same HashMap instance.
source share