Can a constructor be synchronized in Java?



No, a constructor cannot be synchronized in Java. If you try to use the synchronized keyword before a constructor, it may throw a compile-time error in the IDE.

Synchronization is a mechanism used to control the access of multiple threads to any shared resource. When multiple threads access any shared resources, the synchronization prevents data corruption by coordinating access, avoiding race conditions (i.e., occurs when multiple threads concurrently access and modify shared data), and ensuring that operations are performed atomically.

The JVM ensures that only one thread can invoke a constructor call at a given point in time. That is why no need to declare a constructor as synchronized and it is illegal in Java. However, we can use synchronized blocks inside a constructor.

Note! Using a synchronized keyword before a constructor, the compiler says that error: modifier synchronized not allowed here.

Example

In the following, we create a constructor with synchronized modifier, which is an illegal modifier for the constructor that throws an error at compile time:

public class SynchronizedConstructorTest {

   // declaration of synchronized constructor
   public synchronized SynchronizedConstructorTest() {
      System.out.println("Synchronized Constructor");
   }
   
   public static void main(String args[]) {
      SynchronizedConstructorTest test = new SynchronizedConstructorTest();
   }
}t

The above program throws the following error:

Illegal modifier for the constructor in type SynchronizedConstructorTest; only public, protected & private are permittedJava(67109233)

SynchronizedConstructorTest.java:4: error: modifier synchronized not allowed here
   public synchronized SynchronizedConstructorTest() {
                       ^
1 error
Updated on: 2025-08-28T16:19:04+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements