title: Concurrency in Java: synchronized static methods
body: <p>I want to understand how locking is done on static methods in Java.</p>
  
  <p>let's say I have the following class:</p>
  
  <pre><code>class Foo {
      private static int bar = 0;
      public static synchronized void inc() { bar++; }
      public synchronized int get() { return bar; }
  </code></pre>
  
  <p>It's my understanding that when I call <code>f.get()</code>, the thread acquires the lock on the object <code>f</code> and when I do <code>Foo.inc()</code> the thread acquires the lock on the class <code>Foo</code>. </p>
  
  <p>My question is how are the two calls synchronized in respect to each other?
  Is calling a static method also acquires a lock on all instantiations, or the other way around (which seems more reasonable)?</p>
  
  <hr>
  
  <p><strong>EDIT:</strong></p>
  
  <p>My question isn't exactly how <code>static synchronized</code> works, but how does static and non-static methods are synchronized with each other.
  i.e., I don't want two threads to simultaneously call both <code>f.get()</code> and <code>Foo.inc()</code>, but these methods acquire different locks. My question is how is this preventable and is it prevented in the above code.</p>