Sometimes you need to do a calculation on a dataset, where the dataset will be modified. If you only want the result and not modify the dataset, cloning that data is needed. This way, your calculation can work on a copy of the dataset.
Henry [ October 31, 2007: Message edited by: Henry Wong ]
There are times when initializing an object could be too expensive in terms of the resources that such initialization may consume. Therefore, instead of creating a new object, you can clone an existing one and only change the properties that matter to your new instance.
A variation of this last suggestion would be when an object has too many properties, and initializing is too complicated. Therefore, you could initialize a single object, and then reset the properties that matter in every clone.
You might like to study a bit of the Prototype Pattern which seems to use the principle of cloning.
I hope this helps! [ October 31, 2007: Message edited by: Edwin Dalorzo ]
Another important use case is when a class must provide an API that returns a complex object (such as a List) that you do not want to allow the caller to modify. So you can make a clone, and return that. The user/caller can change the clone without changing your internal important list.
Warning! Clones created by the Object clone method are "shallow" clones. References to other objects are duplicated so both original and clone references point to the same object (for example, a List and items on a List). Therefore using a clone does NOT guarantee that your original object will be protected from modification.
[Bill]: Therefore using a clone does NOT guarantee that your original object will be protected from modification.
Or to put it another way, it can guarantee protection from modification, but only if you are sure that the elements within your array/list/collection/whatever are immutable objects. Otherwise you need a deep copy, which generally requires a custom method to loops through and perform whatever action is appropriate to copy the contents. [ November 01, 2007: Message edited by: Jim Yingst ]