I had an interview few weeks back, and I was asked to write a code with Setters and Getters. I had written the following code;
// Just an example
Class ABC{
private int num;
public void setNum(int givenNum){
this.num = givenNum;
}
public int getNum(){
return num;
}
public ABC(){
num = 0;
}
public static void main(String [] args){
ABC object1 = new ABC();
ABC object2 = new ABC();
object1.setNum(5);
System.out.println(object1.getNum());
System.out.println(object2.getNum());
}
}
Now I was told, that "object1" can change the value for "object2.Num". But I was not in agreement with this, I believe another thread which has access to object2 can change the value of object2.Num but not object1.
In the above case, I would have synchronized the setter method, or use the synchronized block inside the setter method while setting/changing the value, but I could not understand the concept of object1 changing the value of object2.Num.
I was just curious if I was missing on something. If so I would really appreciate any help regarding the same.