Interview Questions

Can a constructor be synchronized?

Java Interview Questions and Answers (part 4)


(Continued from previous question...)

54. Can a constructor be synchronized?

No, constructor cannot be synchronized. Because constructor is used for instantiating object, when we are in constructor object is under creation. So, until object is not instantiated it does not need any synchronization.

Enclosing constructor in synchronized block will generate compilation error.

Using synchronized in constructor definition will also show compilation error. COMPILATION ERROR = Illegal modifier for the constructor in type ConstructorSynchronizeTest; only public, protected & private are permitted

Though we can use synchronized block inside constructor.

(Continued on next question...)

Other Interview Questions