The object may be a singleton, but it may be part of a larger structure which doesn't quite know it is. For example, it may implement an interface which can have several different implementations (and thus, instances):
interface StringSink extends Serializable { void dump(String s); }
class MultiDumper implements Serializable {
private final StringSink sink;
public MultiDumper(StringSink sink){ this.sink = sink; }
void doSomeStuff(Collection<String> strings){
for (String s : strings) sink.dump(s);
}
}
Now, let's say we want a StringSink
which dumps strings to stdout. Since there's only one stdout, we might as well make it a singleton:
/** Beware: Not good for serializing! */
class StdoutStringSink {
public static final StdoutStringSink INSTANCE = new StdoutStringSink();
private StdoutStringSink(){}
@Override
public void dump(String s){ System.out.println(s); }
}
And we use it like this:
MultiDumper dumper = new MultiDumper(StdoutStringSink.INSTANCE);
If you were to serialize, and then deserialize this dumper, you'd have two StdoutStringSink
instances floating around in your program.