public class SynchronizedExample { // Synchronized method on the instance (this) public synchronized void syncMethodOnInstance() { // Code that is synchronized on the current instance System.out.println("Synchronized on the instance (this)"); } // Synchronized block on the instance (this) public void syncBlockOnInstance() { synchronized (this) { // Code that is synchronized on the current instance System.out.println("Synchronized block on the instance (this)"); } } // Synchronized method on the class (Class object) public static synchronized void syncMethodOnClass() { // Code that is synchronized on the Class object System.out.println("Synchronized on the class (Class object)"); } // Synchronized block on the class (Class object) public static void syncBlockOnClass() { synchronized (SynchronizedExample.class) { // Code that is synchronized on the Class object System.out.println("Synchronized block on the class (Class object)"); } } // Main method to run the examples public static void main(String[] args) { SynchronizedExample example = new SynchronizedExample(); // Calling instance methods example.syncMethodOnInstance(); example.syncBlockOnInstance(); // Calling static methods syncMethodOnClass(); syncBlockOnClass(); } }