Question: What's the difference between:

```
public synchronized void test(){}

```

and 

```
public void test() {
   synchronized(Sample.class){}
}

```

Answer: 
To make the difference more clear, the first can be rewritten as:

```
public void test() {    
   synchronized(this){    
   }    
}

```

The difference is that the first is synchronized on the *instance* of the class, whereas the second is synchronized on the *class itself*.

In the first case, two threads can simultaneously be executing `test()` on two instances of your class. In the second, they can't.
Declaring a an instance method `synchronized` is just syntactic sugar that's equivalent to having a `synchronized (this)` block. In other words, only a single thread can execute the method **on this instance** at a single point of time.

`synchronized (Sample.class)` means that all instances of this class share a single lock object (the class object itself), and only a single thread can execute this method **on any instance** at a single point in time.