Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

Object clone

Thread view: 
Colin Hemmings - 22 Mar 2006 15:04 GMT
Hi there,
  I have a game that I am currently developing and I am looking to make
exact copies of the gameState object throughout the run of the game. I
need to do this so that I can do move 'look ahead' but not effect the
actual state of the game.

I was looking at using the clone method of Object, but the API says that
clone only performs a "shallow copy" of the object. The object I will be
looking to clone contains a collection of other objects. I want to make
an exact clone of the gameState object that is completely independent of
the original.

Will clone do this or is there a better way?
Joe Attardi - 22 Mar 2006 15:58 GMT
> I was looking at using the clone method of Object, but the API says that
> clone only performs a "shallow copy" of the object. The object I will be
> looking to clone contains a collection of other objects. I want to make
> an exact clone of the gameState object that is completely independent of
> the original.

clone won't do this for you. A shallow copy will still have references
to the same underlying objects, so if you change a mutable object in
the cloned one, the original one will be updated too.
sprit - 22 Mar 2006 16:29 GMT
> > I was looking at using the clone method of Object, but the API says that
> > clone only performs a "shallow copy" of the object. The object I will be
[quoted text clipped - 5 lines]
> to the same underlying objects, so if you change a mutable object in
> the cloned one, the original one will be updated too.

I have been using the object cloner in the following link to do deep
copies for years:
http://www.javaworld.com/javaworld/javatips/jw-javatip76-p2.html
Thomas Weidenfeller - 22 Mar 2006 16:50 GMT
> I was looking at using the clone method of Object, but the API says that
> clone only performs a "shallow copy" of the object.

It is up to you what a clone() method implementation does. If you need a
deep copy, implement clone() that way.

> Will clone do this or is there a better way?

Since you have to implement clone() in your class it will do almost
whatever you want. Almost, because the tricky part is that if you need
to call the clone() methods of existing objects (e.g. collections) or
your superclass, you need to know how these methods are implemented. If
these existing clone() methods only provide a shallow copy, you need to
construct a deep copy of objects in your own code.

You can only do this if the APIs of the existing classes allow yo to
peek at enough of their internal state to construct a deep copy. If they
don't, you probably get enough insight when you subclasses the
particular classes. If the particular classes are final you are in
trouble. Then it would be time for some ugly reflection hack or for
betting on serialization.

/Thomas

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

Oliver Wong - 22 Mar 2006 17:25 GMT
> Hi there,
>   I have a game that I am currently developing and I am looking to make
[quoted text clipped - 9 lines]
>
> Will clone do this or is there a better way?

   You might want to look into making your GameState objects into something
immutable, so as to sidestep the whole issue alltogether. Have it work like
BigInteger or String, such that when you perform an operation (such as "move
player 1 forward 1 square"), rather than modifying the current GameState, it
returns a new GameState object representing the new move.

   - Oliver
Roedy Green - 22 Mar 2006 21:53 GMT
On Wed, 22 Mar 2006 14:04:47 GMT, Colin Hemmings
<colin.hemmings1@ntlworld.com> wrote, quoted or indirectly quoted
someone who said :

>I was looking at using the clone method of Object, but the API says that
>clone only performs a "shallow copy" of the object. The object I will be
>looking to clone contains a collection of other objects. I want to make
>an exact clone of the gameState object that is completely independent of
>the original.

if you serialize an object, you get a deep copy. If you restore it,
you have an reasonably independent clone. Some of the strings might be
common, but that won't cause trouble.

It is an easy way to get a deep copy, albeit with a high overhead.

see http://mindprod.com/jgloss/serialization.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.