title: What is the purpose of passing parameter to synchronized block?
body: <p>I know that</p>
  
  <blockquote>
    <p>When you synchronize a block of code, you specify which object's lock
    you want to use as the lock, so you could, for example, use some
    third-party object as the lock for this piece of code. That gives you
    the ability to have more than one lock for code synchronization within
    a single object.</p>
  </blockquote>
  
  <p>However, I don't understand the need of passing argument to the block. Because it doesn't matter whether I pass String's instance, Some random class's instance to the synchronized block as the synchronized block works perfectly irrespective of the parameter being passed to the block.</p>
  
  <p>So my question is <strong>if anyways synchronized block stops two threads from entering the critical section simultaneously. Then why there is a need of passing an argument. (I mean acquire lock on some random object by default).</strong></p>
  
  <p>I hope I framed my question correctly.</p>
  
  <p>I have tried the following example with random parameters being to the synchronized block.</p>
  
  <pre><code>public class Launcher {
  
      public static void main(String[] args) {
          AccountOperations accOps=new AccountOperations();
  
          Thread lucy=new Thread(accOps,"Lucy");
          Thread sam=new Thread(accOps,"Sam");
  
          lucy.start();
          sam.start();
  
      }
  
  }
  </code></pre>
  
  <p>Using non-static synchronized block:</p>
  
  <pre><code>public class AccountOperations implements Runnable{
      private  Account account = new Account();
  
  
      public void run(){
  
          for(int i=0;i&lt;5;i++){
  
              makeWithdrawal(10);                 
          }
      }
  
      public  void makeWithdrawal(int amount){
          String str="asd"
          synchronized (str /* pass any non-null object the synchronized block works*/) {
              if(account.getAmount()&gt;10){
  
                  try{
                      Thread.sleep(5000);             
                  }catch(InterruptedException e){
                      e.printStackTrace();
                  }
                  account.withdraw(amount);
                  System.out.println(Thread.currentThread().getName()+" has withdrawn 10, current balance "+ account.getAmount());
              }else{
                  System.out.println("Insufficient funds "+account.getAmount());
              }
          }
  
      }
  
  }
  </code></pre>
  
  <p>Using static synchronized block:</p>
  
  <pre><code>public class AccountOperations implements Runnable{
      private static Account account = new Account();
  
  
      public void run(){
  
          for(int i=0;i&lt;5;i++){
  
              makeWithdrawal(10);                 
          }
      }
  
      public static void makeWithdrawal(int amount){
  
          synchronized (String.class /* pass any class literal synchronized block works*/) {
              if(account.getAmount()&gt;10){
  
                  try{
                      Thread.sleep(5000);             
                  }catch(InterruptedException e){
                      e.printStackTrace();
                  }
                  account.withdraw(amount);
                  System.out.println(Thread.currentThread().getName()+" has withdrawn 10, current balance "+ account.getAmount());
              }else{
                  System.out.println("Insufficient funds "+account.getAmount());
              }
          }
  
      }
  
  }
  </code></pre>