Results 1 to 8 of 8
- 05-24-2012, 06:13 PM #1
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
How does the clone() method work?
I've decided to spend a little time each day before programming practice to learn the api... what better place to start than the object class from which all things derive.
now I know clone() should not be used, and object serilization should be used instead (whatever that is) but this is theoretical and the definition confuses me
the default implementation returns a shadow copy.
instance variables values in one object are copied to another of the same type.
for reference types only the references are copied.
instance values of an object? isnt that object then a reference type... if I'm not mistaken isnt a ref type a class or array, how can its value be copied if it then goes on to say only a reference to it is copied.
I thought this would make b another reference to a, but thats not true because it cant find feild num which exists in a.Java Code:Workshop a = new Workshop(); Workshop b = new Workshop(); a.num = 1; b = (Workshop) a.clone(); System.out.println(b.num);
perhapes someone can be more clear on waht clone doesLegend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.
- 05-24-2012, 06:25 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,479
- Rep Power
- 16
Re: How does the clone() method work?
What actually happens when you try to compile and/or run that code?
WHat errors (copy and pasted here) do you get?Please do not ask for code as refusal often offends.
- 05-24-2012, 07:44 PM #3
- 05-24-2012, 08:18 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: How does the clone() method work?
The original intention of the clone() method was that you could copy or clone any object of any type you want. Sometimes you don't want to clone your objects (of a certain class) so you also had to implement an empty interface to indicate that you do want your objects cloned. You still have to override the clone() method. imho all that hassle isn't worth the trouble; i.e. a copy constructor can do the job too; you do have to know the class of the object to be copied though ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-25-2012, 03:11 PM #5
Re: How does the clone() method work?
Yeah, all objects get the clone() method since Object has it, but java has no idea what fields and data should be cloned unless you define it yourself by overriding the clone method the way Jos described.
There is no magic method in java that gives you a perfect copy of an instance (from a class you wrote yourself) without you doing a little work to tell it how to copy it. This usually isn't a problem though, because object cloning really isn't done very much (at least not in the work I've ever done) and implementing a clone() method or a copy constructor isn't more than a few lines of simple assignment code.
- 05-25-2012, 03:31 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: How does the clone() method work?
Cloning can involve some quite nasty details; suppose there are two objects A and B (both of the same type) where A points to/refers to B; i.e. A ---> B (they are graph vertex objects); now if we clone A (call it A') we end up wit a situation like: A ---> B <--- A' because cloning is a shallow operation. Suppose we don't want this, so we clone every member of object A as well; we end up with this: A ---> B, A' ---> B'. But now suppose B also refers to A (i.e. its a circular graph). Better duck and hide then ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-25-2012, 03:42 PM #7
Re: How does the clone() method work?
Excellent point Jos, you nicely illustrated what I meant when I said
. This is exactly what I meant!java has no idea what fields and data should be cloned
- 05-25-2012, 07:05 PM #8
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Re: How does the clone() method work?
Some very interesting responses, I got out my notepad for this (especially joes example). thanks guys its clearer now
Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.
Similar Threads
-
Overriding .clone() Method
By Moncleared in forum New To JavaReplies: 1Last Post: 02-21-2011, 02:57 PM -
help~ delete method cant work
By reeveliew in forum New To JavaReplies: 4Last Post: 05-07-2010, 02:24 AM -
Clone method question
By frenk_castle in forum New To JavaReplies: 3Last Post: 04-08-2010, 08:17 PM -
clone method
By javaplus in forum New To JavaReplies: 2Last Post: 01-30-2008, 09:47 AM -
clone method
By gapper in forum New To JavaReplies: 1Last Post: 01-20-2008, 08:46 AM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks