let's consider the following code:
public class Test {
private int a;
public Test(){
this.a = 1;
}
public static void main(String[] args) {
Test A = new Test();
Test B = A;
// TEST1
B.a = 0;
System.out.println(A); // prints 0
// TEST2
B = null;
System.out.println(A); // also prints 0
}
public String toString(){
return Integer.toString(this.a);
}
}
In TEST1, we modify B
and then A
is modified because B
points to A
.
I may understand why in TEST2 A
doesn't become null
, because B
now points to null
.
But the results of the two tests seem contradictory: why A
doesn't become null ?
Thanks.