0

With reference to the Java tutorials at Oracle (https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html), below is a copy of the part which confuses me totally. Can someone be kind enough to explain it in layman terms for me?

For example, consider a method in an arbitrary class that moves Circle objects:

public void moveCircle(Circle circle, int deltaX, int deltaY) {
    // code to move origin of circle to x+deltaX, y+deltaY
    circle.setX(circle.getX() + deltaX);
    circle.setY(circle.getY() + deltaY);

    // code to assign a new reference to circle
    circle = new Circle(0, 0);
}

Let the method be invoked with these arguments:

moveCircle(myCircle, 23, 56)

Inside the method, circle initially refers to myCircle. The method changes the x and y coordinates of the object that circle references (i.e., myCircle) by 23 and 56, respectively.

These changes will persist when the method returns. Then circle is assigned a reference to a new Circle object with x = y = 0. This reassignment has no permanence, however, because the reference was passed in by value and cannot change.

Within the method, the object pointed to by circle has changed, but, when the method returns, myCircle still references the same Circle object as before the method was called.

Sreehari
  • 5,621
  • 2
  • 25
  • 59
Scorpiorian83
  • 469
  • 1
  • 4
  • 17

0 Answers0