Object clone

Discussion in 'Java' started by Colin Hemmings, Mar 22, 2006.

  1. 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?
    Colin Hemmings, Mar 22, 2006
    1. Advertisements

  2. Colin Hemmings

    Joe Attardi Guest

    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.
    Joe Attardi, Mar 22, 2006
    1. Advertisements

  3. Colin Hemmings

    sprit Guest

    I have been using the object cloner in the following link to do deep
    copies for years:
    sprit, Mar 22, 2006
  4. It is up to you what a clone() method implementation does. If you need a
    deep copy, implement clone() that 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 Weidenfeller, Mar 22, 2006
  5. Colin Hemmings

    Oliver Wong Guest

    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
    Oliver Wong, Mar 22, 2006
  6. Colin Hemmings

    Roedy Green Guest

    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
    Roedy Green, Mar 22, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.