title: Static versus non-static lock object in synchronized block
body: <p>Trying to visualize and understand <em>synchronization</em>. </p>
  
  <ol>
  <li>What are the differences between using a <strong><em>static lock object</strong> (code A)</em> and a <strong><em>non-static lock object</strong> (code B)</em> for a <em>synchronized block</em>? </li>
  <li>How does it differ in practical applications? </li>
  <li>What are the pitfalls one would have that the other wouldn't? </li>
  <li>What are the criteria to determine which one to use?</li>
  </ol>
  
  <p><strong>Code A</strong></p>
  
  <pre><code>public class MyClass1 {
    private static final Object lock = new Object();
    public MyClass1() {
      //unsync
      synchronized(lock) {
        //sync
      }
      //unsync
    }
  }
  </code></pre>
  
  <p><strong>Code B</strong></p>
  
  <pre><code>public class MyClass2 {
    private final Object lock = new Object();
    public MyClass2() {
      //unsync
      synchronized(lock) {
        //sync
      }
      //unsync
    }
  }
  </code></pre>
  
  <p><strong>Note</strong> </p>
  
  <p>The above code shows constructors, but you could talk about how the behavior is different in a static method and a non-static method too. Also, would it be advantageous to use a static lock when the synchronized block is modifying a static member variable?</p>
  
  <p>I already looked at answers in <a href="https://stackoverflow.com/questions/8774442">this question</a>, but it's not clear enough what the different usage scenarios are.</p>