Mastering Java: The Ultimate Quiz for 'Thinking in Java'

Disable ads (and more) with a membership for a one time $2.99 payment

Want to excel in Java? Test your knowledge with our ultimate quiz based on 'Thinking in Java'. Engage with multiple-choice questions and in-depth explanations. Boost your understanding and prepare for your exam with confidence.

Each practice test/flash card set has 50 randomly selected questions from a bank of over 500. You'll get a new set of questions each time!

Practice this question and more.


Why might a class use a private constructor?

  1. To prevent subclassing

  2. To implement static factory methods

  3. To allow instantiation only inside the class

  4. All of the above

The correct answer is: All of the above

Option A is incorrect because a private constructor does not necessarily prevent subclassing, it only restricts direct access to the constructor. A subclass could still access the superclass' private constructor through a public or protected method. Option B is incorrect because static factory methods can be implemented using public constructors. Option C is incorrect because a private constructor does not necessarily mean the class can only be instantiated from inside the class. Other static methods in the class could also create an instance using the private constructor. A private constructor may be used for a combination of these reasons, but not exclusively for any one reason.