aspose file tools*
The moose likes Beginning Java and the fly likes shallow cloning Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "shallow cloning" Watch "shallow cloning" New topic
Author

shallow cloning

priyanaka jaiswal
Ranch Hand

Joined: Jun 03, 2011
Posts: 79
Hi All,


How to achieve shallow cloning in java?


Thanks in advance
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

priyanaka jaiswal wrote:How to achieve shallow cloning in java?

Usually with java.lang.Cloneable and clone().

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1773
    
    7

While that is true, I personally avoid the Cloneable interface for new types, if at all possible.
I'd much rather just implement a copy-constructor or some factory method.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Jelle Klap wrote:While that is true, I personally avoid the Clonable interface for new types, if at all possible.
I'd much rather just implement a copy-constructor or some factory method.

I totally agree, but the question, as yet, isn't very clear.

And clone() does sometimes have the virtue of brevity.

Winston
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Winston Gutkowski wrote:
Jelle Klap wrote:While that is true, I personally avoid the Clonable interface for new types, if at all possible.
I'd much rather just implement a copy-constructor or some factory method.

I totally agree, but the question, as yet, isn't very clear.

And clone() does sometimes have the virtue of brevity.

Winston


And with clone() you get polymorphism--you can get a copy without having to care which implementation class you have.
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

I'm with Jeff . With clone() you can get a copy without knowing the class of the object being copied. There was an article many years ago (I can't find it now) on this subject that came down on the side of clone() but these days I gather the tide has turned. I wonder how the 'prototype' pattern is implemented using copy constructors?
Ritesh raushan
Ranch Hand

Joined: Aug 29, 2012
Posts: 100


Generally clone method of an object, creates a new instance of the same class and copies all the fields to the new instance and returns it.
This is nothing but shallow copy. Object class provides a clone method and provides support for the shallow copy.
It returns ‘Object’ as type and you need to explicitly cast back to your original object.

Since the Object class has the clone method (protected) you cannot use it in all your classes.
The class which you want to be cloned should implement clone method and overwrite it.
It should provide its own meaning for copy or to the least it should invoke the super.clone().
Also you have to implement Cloneable marker interface or else you will get CloneNotSupportedException.
When you invoke the super.clone() then you are dependent on the Object class’s implementation and what you get is a shallow copy.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Ritesh raushan wrote:The process of creating an exact copy of an existing copy of an existing object is called cloning...

There are two types of cloning..


Actually there are as many types for a given class as there are combinations of member objects in that class and all its contained objects. We can clone some or all of the members 1, 2, or N layers deep. (Not that we should do that, but the option is there.

But yes, usually we talk only about the two extremes.

1-deep cloning(when the cloned object is modified ,if the original object is not modified,then is called deep cloning).

2-shallow cloning(when the cloned object is modified,same modification will also affect the original object called shallow cloning)...


The modification effects you mention are consequences of the two types of cloning, not the definitions.
 
wood burning stoves
 
subject: shallow cloning