I have this code. When I run it I get this result:
The Circle A : The center X: 5, the center Y 5 and the Radius 5
The Circle B : The center X: 10, the center Y 10 and the Radius 10
Change
The Circle A : The center X: 10, the center Y 10 and the Radius 5
The Circle B : The center X: 5, the center Y 5 and the Radius 10
if java does not allow to pass values as reference, how did this code allow it?
Just in case, when I look it with the debugger the direction of the object "autre" was the same as the object A.
public class Main {
public static void main(String[] args) {
Cercle A = new Cercle();
Cercle B = new Cercle();
A.creer(5, 5, 5);
System.out.println("The Circle A : ");
A.show();
B.creer(10, 10, 10);
System.out.println("The Circle B : ");
B.show();
System.out.println("Change");
B.change(A);
System.out.println("The Circle A : ");
A.show();
System.out.println("The Circle B : ");
B.show();
}
}
public class Cercle {
public int x, y;
public int r;
public void creer (int n1, int n2, int n3)
{
x = n1;
y = n2;
r = n3;
}
public void show()
{
System.out.println(" The center X: "+x+", the center Y "+y+" and the Radius "+r);
}
public void change(Cercle autre){
int tmp ;
tmp = x ;
x = autre.x;
autre.x = tmp;
tmp = y;
y = autre.y;
autre.y = tmp ;
}
}