title: Can a shared resource safely be used as the lock for a synchrnized block?
body: <p>I often find myself with code like</p>
  
  <pre><code>private static final MyType sharedResource = MyType();
  private static final Object lock = new Object();
  ...
  synchronized(lock)
  {
      //do stuff with sharedResource
  }
  </code></pre>
  
  <p>Is this really neccessary or could sharedResource be used as the lock itself like</p>
  
  <pre><code>private static final MyType sharedResource = MyType();
  ...
  synchronized(sharedResource)
  {
      //do stuff with sharedResource
  }
  </code></pre>
  
  <p><em>Note: synchronized blocks shown in the examples would live within methods doing work and are not methods themselves or synchronized methods.</em></p>
  
  <p><strong>EDIT:</strong> A very good point has been pointed out in some of the answers that if we are dealing with multiple shared resources that the first "Object" technique is far safer.</p>
  
  <p><strong>WARNING</strong> The fact that sharedResource is <code>static</code> is important! If it is <code>static</code> then synchronized methods or synchronized blocks locking on <code>this</code> won't work. The lock object must also be <code>static</code>.</p>