For instance,
public Foo{
private Object mutex = new Object();
private int bar;
public Foo(Foo f){
this.mutex = f.getMutex();
this.bar = f.getBar();
}
public Object getMutex(){
return mutex;
}
public void setBar(int bar){
synchronized(mutex){
this.bar = bar;
}
}
public int getBar(){
synchronized(mutex){
return bar;
}
}
}
It depends – do you want the two objects to share a mutex (a shallow copy, really) or do you want them to be independent? In most cases I’d expect the latter, in which case you wouldn’t want to copy the reference.