aspose file tools*
The moose likes Java in General and the fly likes what is the need for cloning a object ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "what is the need for cloning a object ?" Watch "what is the need for cloning a object ?" New topic
Author

what is the need for cloning a object ?

Thennam Pandian
Ranch Hand

Joined: Oct 11, 2005
Posts: 163
what is the need for cloning a object ? give me a practial example.

so that i can understand very deeply.

what is the advantage of clonning ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18709
    
  40

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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
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 ]
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

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.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
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.

Thats why people write custom clone methods.

Bill
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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 ]

"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is the need for cloning a object ?