2 Replies - 417 Views - Last Post: 20 August 2019 - 11:29 AM Rate Topic: -----

#1 tekblade   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 21-December 17

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

Posted 20 August 2019 - 03:46 AM

Hi, I have such problem with the code. After this few instructions there are trashes in firstElement secound etc...

public class Element {
    int x;
    int y;
    public Element(){
    
    }
    public Element clone(){
        Element newCloneElement = new Element();
        newCloneElement.x = x;
        newCloneElement.y = y;
        return newCloneElement;
    }
 
    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;
    }
}



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


        element1.setX(firstPoint.getX()-1);
        element1.setY(firstPoint.getY());
        element2.setX(firstPoint.getX()+1);
        element2.setY(firstPoint.getY());
        element3.setX(secoundPoint.getX()+1);
        element3.setY(secoundPoint.getY());
        element4.setX(thirdPoint.getX());
        element4.setY(thirdPoint.getY()+1);
    if((MainActivity.ELEMENTS-1-firstPoint.getY()<2) )
        return;
    if(HelperMethods.detectTurnColision(element1,element2,
        element3,element4)){
            return;
    }  
    if(HelperMethods.detectTurnRightWallColision(element1,
        element2,element3,element4)){
            return;
    }
    //TRASHES 
    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());



The issue is that there are not any compilation error - my points aren't having the proper values as they should

For example this shows proper values:
        Element element1=firstPoint;
        Element element2=secoundPoint;
        Element element3=thirdPoint;
        Element element4=fourthPoint;
        element1.setX(firstPoint.getX()-1);
        element1.setY(firstPoint.getY());
        element2.setX(firstPoint.getX()+1);
        element2.setY(firstPoint.getY());
        element3.setX(secoundPoint.getX()+1);
        element3.setY(secoundPoint.getY());
        element4.setX(thirdPoint.getX());
        element4.setY(thirdPoint.getY()+1);
        firstPoint=element1;
        secoundPoint=element2;
        thirdPoint=element3;
        fourthPoint=element4;


This post has been edited by tekblade: 20 August 2019 - 11:43 AM


Is This A Good Question/Topic? 0
  • +

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

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6823
  • View blog
  • Posts: 28,274
  • Joined: 12-December 12

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

Posted 20 August 2019 - 04:26 AM

You have been here long enough to know that you must provide a clear question and provide any error details. What happens when you run your code? What errors are there? On what line(s)? Etc..

Also, a problem with your code is not a discussion topic. Moving from Discussion Lounge to Java forum.
Was This Post Helpful? 0
  • +
  • -

#3 tekblade   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 21-December 17

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

Posted 20 August 2019 - 11:29 AM

It can be closed

This post has been edited by tekblade: 21 August 2019 - 07:31 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1