package main;
public class Main {
double radius;
public Main(double newRadius) {
radius = newRadius;
}
public static void main (String [] args) {
Main x = new Main(1);
Main y = new Main(2);
Main temp;
// try to swap first time
temp = x;
x = y;
y = temp;
System.out.println(x.radius + " " + y.radius);
x = new Main(1);
y = new Main(2);
// try to swap second time
swap(x, y);
System.out.println(x.radius + " " + y.radius);
}
public static void swap(Main x, Main y) {
Main temp = x;
x = y;
y = temp;
}
}
Why the first time worked, but the second time didn't? The first one did the swap, but the second one didn't. I'm passing the reference to the function. Why this doesn't work?