I understand what synchronize and volatile does and where they are used. I just learned volatile and I was confused about when could we use synchronize without volatile. if I synchronize an object I need to block other threads from using the same object , but in most cases I would do that to edit the object, if so I need to have volatile on that attributes I'm editing .
The following code is about race condition and I wonder why I've never seen someone using volatile on count variable :
public synchronized void add(int value){
this.count += value;
}
shouldn't be count volatile here ?
I'm just trying to figure a case where synchronize can be used without volatile , a piece of code will help.