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.
2
If class ABC can be accessed from more than one thread, you will need to synchronize both setNum
and getNum
. Until you leave a synch block, the new value of num
need not be available to any other thread. Until you enter a synch block, changes so made in another thread will not be visible in this one. And in Java (but not C#), both both blocks must (according to the rules, the JLS) be synched on the same object. (Of course, if you make the methods synchronized
, they’ll synch nicely on this instance of ABC.)
This might have been the point your interviewer was making.
(And making ‘num’ volatile (private volatile int num
) would do much the same thing.)
4
If all you have is a reference to object1
, then no, you can’t change object2.num
.
The only thing that comes somewhat close to what you describe would be things like this:
public void swap(ABC other) {
int num = this.num;
this.num = other.num;
other.num = num
}
Suche a code could modify two ABC
objects: this
(i.e. the object on which it’s called) and other
(the parameter). But this doesn’t have anything to do with multithreading either.
3
Synchronization is not the relevant concept here. You synchronize things to prevent errors caused by the unforeseen order of actions caused by interleaving different threads. The classical example is decreasing the balance of one bank account and increasing another by the same amount, which can lose information (money) if not done atomically. But this code merely assigns an integer, which is already atomic, so the point is not inconsistency.
They were probably talking about access control and the somewhat surprising fact that private
means “private to all instances of the class” and not “private to to the owning instance only” in java. object2
can change the value of a field in object1
if it has a reference to object1
. But this has nothing to do with synchronization.
1
that “object1” can change the value for “object2.Num”
That’s impossible, what is possible though is one thread to set a value on either of the instances (object1
or object2
) and before using it another thread to set a different value on the same instance.
If each thread has to work with its own values then change the num
type from int
to ThreadLocal<Integer>
without changing the getter and setter signature.
If each thread has to work with same value then change the num
type from int
to AtomicReference<Integer>
and the getter and setter signature.