Originally posted by Campbell Ritchie:
I know you will call me pedantic, but you aren't using serialisation to implement clone(). You can use it to circumvent clone(). As you say, people rarely use clone() anyway.
I think you can do either, really. It's probably more common to use this technique to circumvent clone(), as you say. But you could also use this technique to provide a clone() implementation that gives a deep copy, without having to detail all the fields yourself. This might make sense for a base class that will be extended by many others, where you want deep copies and you don't want to have to write all those clone() methods individually. And you don't care if your clone() method is notably slower than other common implementations would be. (Like most other performance issues, most of the time it doesn't really matter anyway.) I agree that clone() isn't used that often anyway, and this usually wouldn't be a good way to implement it. But sometimes it might make sense.