If I am using synchronized
, then does the object which is being synchronized have to be static
?
EDIT: I meant to say reference to objects must be static. I noticed that one of the examples I was reading stated that in order for threads to use a synchronized method in class A, then the reference to class A must be static.
So, I was wondering whether it is a rule to make the reference to an object static, so that all the threads which call the synchronized method are using the same copy of instance. In my example, the synchronized method is used to count from 1 to 10. So each thread accessing this synchronized method should each count 1 to 10. I tried this without static and the results were incorrect.