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.


How can you create a class object if its constructor is private?

  1. By inheriting the class

  2. By creating an object in a subclass

  3. By calling a public static method of the class meant for object creation

  4. You cannot create an object if the constructor is private

The correct answer is: By calling a public static method of the class meant for object creation

Creating a class object requires calling the constructor of the class. However, if the constructor is set to private, it cannot be called outside of the class. Therefore, options A and B are incorrect as they involve inheriting or subclassing the class, both of which still require calling the constructor. Option D is also incorrect as it provides a general statement without explaining why the constructor being private is significant. Option C is the correct answer as it suggests using a public static method of the class specifically designed for object creation, bypassing the private constructor restriction.