title: What is the synchronized method in Java
body: <p>What is the main idea of synchronized method and synchronized block in Java?</p>
  
  <p>And why should we use them?</p>
  
  <p>Sample code would be nice. I have read the Java documentation about synchronized method, but I didn't get the idea.</p>
  
  <p>This is the Java documentation code</p>
  
  <pre><code>public class SynchronizedCounter {
      private int c = 0;
  
      public synchronized void increment() {
          c++;
      }
  
      public synchronized void decrement() {
          c--;
      }
  
      public synchronized int value() {
          return c;
      }
  }
  </code></pre>