What is the access modifier of the Singleton's constructor?

Prepare for the Object-Oriented Programming Test with flashcards and multiple choice questions. Each question comes with hints and explanations to enhance understanding. Master OOP concepts and succeed in your exam!

Multiple Choice

What is the access modifier of the Singleton's constructor?

Explanation:
To enforce a single instance, the constructor is made private. A private constructor prevents code outside the class from creating new objects, so the class can control exactly how and when an instance is created—usually by creating one static instance inside the class and returning it through a static method like getInstance. If the constructor were public, anyone could call new and create multiple instances, defeating the singleton guarantee. A protected or default (package-private) constructor would similarly allow other code (in the same package or in a subclass) to instantiate the class, which also breaks the pattern. Hence, the private modifier is the correct choice to preserve a true singleton.

To enforce a single instance, the constructor is made private. A private constructor prevents code outside the class from creating new objects, so the class can control exactly how and when an instance is created—usually by creating one static instance inside the class and returning it through a static method like getInstance. If the constructor were public, anyone could call new and create multiple instances, defeating the singleton guarantee. A protected or default (package-private) constructor would similarly allow other code (in the same package or in a subclass) to instantiate the class, which also breaks the pattern. Hence, the private modifier is the correct choice to preserve a true singleton.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy