Please review the following code:
public class St1 {
public static void main(String[] args) {
Boolean b = true;
C1 c1 = new C1(b);
C2 c2 = new C2(b);
System.out.println(c1.getB());
System.out.println(c2.getB());
}
}
public class C1 {
private Boolean m_b;
public Boolean getB() {
return m_b;
}
public C1(Boolean b) {
m_b = b;
m_b = false;
}
}
public class C2 {
private Boolean m_b;
public Boolean getB() {
return m_b;
}
public C2(Boolean b) {
m_b = b;
}
}
So the output is:
false
true
But I fail to understand why? All I do is passing and modifying a Boolean object which means I pass its reference or address value. Assignments of objects are also references assignments, aren't they?
So, I understand that I am dealing all the time with the same object. Probably I am wrong. But where? Why changing its value in C1 doesn't affect its value in C2?