title: How to use synchronized blocks across classes?
body: <p>I want to know how to use synchronized blocks across classes. What I mean is, I want to have synchronized blocks in more than one class but they're all synchronizing on the same object. The only way that I've thought of how to do this is like this:</p>
  
  <pre><code>//class 1
  public static Object obj = new Object();
  
  someMethod(){
       synchronized(obj){
           //code
       }
  }
  
  
  //class 2
  someMethod(){
       synchronized(firstClass.obj){
           //code
       }
  }
  </code></pre>
  
  <p>In this example I created an arbitrary Object to synchronize on in the first class, and in the second class also synchronized on it by statically referring to it. However, this seems like bad coding to me.
  Is there a better way to achieve this?</p>