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

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

Mastering Java: The Ultimate Quiz for 'Thinking in Java'. Dive deep into Java with multiple-choice questions. Challenge yourself with quiz questions designed to test and improve your understanding of the 'Thinking in Java' book. Get ready for your exam!

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.