Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java - Copy an Object

  • 17-11-2009 3:21pm
    #1
    Registered Users, Registered Users 2 Posts: 9,211 ✭✭✭


    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,101 ✭✭✭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 Posts: 2,013 ✭✭✭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,211 ✭✭✭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