title: jvm differences between synchronized and non-synchronized methods
body: <p>I have the following class:</p>
  
  <pre><code>public class SeqGenerator {
  
      int last = 0;
      volatile int lastVolatile = 0;
  
      public int getNext() {
          return last++;
      }
  
      public synchronized int getNextSync() {
          return last++;
      }
  
      public int getNextVolatile() {
          return lastVolatile++;
      }
  
      public void caller() {
          int i1 = getNext();
          int i2 = getNextSync();
          int i3 = getNextVolatile();
      }
  
  }
  </code></pre>
  
  <p>When I look at the disassembled code I don't see the difference between the representation of three methods <code>getNext()</code>, <code>getNextSync()</code> and <code>getNextVolatile()</code> .</p>
  
  <pre><code>public int getNext();
    Code:
     0:   aload_0
     1:   dup
     2:   getfield    #2; //Field last:I
     5:   dup_x1
     6:   iconst_1
     7:   iadd
     8:   putfield    #2; //Field last:I
     11:  ireturn
  
  public synchronized int getNextSync();
    Code:
     0:   aload_0
     1:   dup
     2:   getfield    #2; //Field last:I
     5:   dup_x1
     6:   iconst_1
     7:   iadd
     8:   putfield    #2; //Field last:I
     11:  ireturn
  
  public int getNextVolatile();
    Code:
     0:   aload_0
     1:   dup
     2:   getfield    #3; //Field lastVolatile:I
     5:   dup_x1
     6:   iconst_1
     7:   iadd
     8:   putfield    #3; //Field lastVolatile:I
     11:  ireturn
  
  public void caller();
    Code:
     0:   aload_0
     1:   invokevirtual   #4; //Method getNext:()I
     4:   istore_1
     5:   aload_0
     6:   invokevirtual   #5; //Method getNextSync:()I
     9:   istore_2
     10:  aload_0
     11:  invokevirtual   #6; //Method getNextVolatile:()I
     14:  istore_3
     15:  return
  </code></pre>
  
  <p>How the JMV can distinguish between these methods?</p>
  
  <p>The generated code is the same of these methods and also of their callers. How the JVM performs the synchronization? </p>