Search code examples
javaclonepass-by-reference

The way to clone object in java by value - not by reference


I want to copy my Object of class implemented by me not as reference order by value.

My code sometimes behaves in strange way.

This is my class Element

public class Element {
    int x;
    int y;
    public Element(){

    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }
}

Example code that I am using:

public void fourthTurnElement6(){
    MainLoopClass.helpTurn=false;
    Element element1=firstPoint;
    Element element2=secoundPoint;
    Element element3=thirdPoint;
    Element element4=fourthPoint;
    element1.setX(firstPoint.getX());
    element1.setY(firstPoint.getY()+1);
    element2.setX(firstPoint.getX()+1);
    element2.setY(firstPoint.getY());
    element3.setX(firstPoint.getX()+1);
    element3.setY(firstPoint.getY()-1);
    element4.setX(thirdPoint.getX()+1);
    element4.setY(thirdPoint.getY());


    if(HelperMethods.detectTurnColision(element1,element2,
        element3,element4)){
            return;
    }

    if(HelperMethods.detectTurnRightWallColision(element1,
        element2,element3,element4)){
            return;
    }
    firstPoint=element1;
    secoundPoint=element2;
    thirdPoint=element3;
    fourthPoint=element4;

Is it possible that sometimes it will work proper but sometimes not? It seems in my code that detectTurnColision works fine but the issue is detectTurnRightWallColision. The second function is very simple but I don't think the error occurs there.

EDIT: Do you see any error here:

    Element element1=firstPoint.clone();
    Element element2=secoundPoint.clone();
    Element element3=thirdPoint.clone();
    Element element4=fourthPoint.clone();


    element1.setX(firstPoint.getX());
    element1.setY((firstPoint.getY()));
    element2.setX(firstPoint.getX());
    element2.setY(firstPoint.getY()+1);
    element3.setX(firstPoint.getX());
    element3.setY(firstPoint.getY()+2);
    element4.setX(firstPoint.getX()-1);
    element4.setY(firstPoint.getY()+2);
    if((MainActivity.ELEMENTS-1-firstPoint.getY()<2) )
        return;
    if(HelperMethods.detectTurnColision(element1,element2,
        element3,element4)){
            return;
    }  
    if(HelperMethods.detectTurnRightWallColision(element1,
        element2,element3,element4)){
            return;
    }
    firstPoint.setX(element1.getX());
    firstPoint.setY(element1.getY());
    secoundPoint.setX(element2.getX());
    secoundPoint.setY(element2.getY());
    thirdPoint.setX(element3.getX());
    thirdPoint.setY(element3.getY());
    fourthPoint.setX(element4.getX());
    fourthPoint.setY(element4.getY());

Solution

  • Regarding the 'copy' part you can create a copy constructor or method in Element Class.

    constructor :

    public Element(Element element){
        this.x = element.x
        this.y = element.y
    }
    // ToDo handle null argument if needed if(element==null)return; 
    // values will be set to 0.
    

    example:

    Element element1 = new Element();
    Element element2 = new Element(element1);
    

    method :

    public Element clone(){
        Element newCloneElement = new Element();
        newCloneElement.x = x;
        newCloneElement.y = y;
        return newCloneElement;
    }
    // The above method can implemented as static with an Element argument
    // and the values will be copied from the argument.
    

    example:

    Element element1 = new Element();
    Element element2 = element1.clone();
    

    I can not provide answer to the second question because you do not explain what the method does.