Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java - Copy an Object

  • 17-11-2009 04:21PM
    #1
    Registered Users, Registered Users 2 Posts: 9,394 ✭✭✭


    I need to clone an object in java but after some initial playing around and some reading up, it seems using object.clone() gives me a shallow copy, which is of no use to me. I've read that overriding the object.clone() method can give me a deep copy, but I'm not entirely sure what I'm supposed to do when I override object.clone().

    The object I want to copy is :
    public class GameBoard implements Cloneable {
    	private GamePiece[] board;
    	private int[][] redPegs;
    	private int[][] bluePegs;
    	private int[] unplayableTiles;
    	private int[] redTopRow;
    	private int[] redBottomRow;
    	private int[] blueTopRow;
    	private int[] blueBottomRow;
    	private Vector<Integer> bluePieces;
    	private Vector<Integer> redPieces;
    	private Vector<Integer> emptyPieces;
    	private boolean redsTurn;
             }
    

    If I just clone that without overriding the object.clone() method, changes I make to the emptyPieces vector in the cloned GameBoard also happen in the original GameBoard. I want the two GameBoard objects to be completely independent of each other.

    There's also an array of type GamePiece in there. Would I have to also clone all the GamePieces in the array? As in, if I just clone the array itself, will the cloned array and original array actually just point to the same GamePiece objects?

    GamePiece looks like:
    enum stateType {
        BLUE, RED, NONE, UNUSED
    }
    
    public class GamePiece implements Cloneable{
    	private stateType state;
    }
    


Comments

  • Registered Users, Registered Users 2 Posts: 1,126 ✭✭✭Rulmeq


    I generally refer to Joshua Bloch's opinion on these things, and he recommends that you avoid clone:

    http://www.artima.com/intv/bloch13.html


  • Registered Users, Registered Users 2, Paid Member Posts: 2,032 ✭✭✭lynchie


    The default implementationof Cloneable is indeed a shallow copy. Instead use Serialization and the writeObjcet and readObject methods of a ObjectOutputStream to perform a full deep copy. It can be done in a few lines of code. Google it and you should see some sample code.


  • Registered Users, Registered Users 2 Posts: 9,394 ✭✭✭Royale with Cheese


    Right, I started making a copy constructor but there was a lot of work I would have had to have done because of the way I'd implemented the class. Ended up using serialization and that seems to have done the trick, although I have tested it fully yet.


  • Registered Users, Registered Users 2 Posts: 5,618 ✭✭✭Civilian_Target


    Like Rulmeq says, you should really avoid clone, if you're doing things cleanly you shouldn't ever need it, just set fields explicitly.

    If you do need to clone, serialize / deserialize works fine. But if you're using clone extensively, implement the writeExternal and readExternal methods of your frequently cloned objects and their children, to avoid a performance hit.


Advertisement