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.


Can you create an object of an inner class without an instance of the outer class?

  1. Yes

  2. No

  3. Only if the inner class is static

  4. Only if the constructor is public

The correct answer is: Only if the inner class is static

An inner class is a class within another class. It can only be accessed through an instance of the outer class. This means that to create an object of an inner class, an instance of the outer class must first be created. Therefore, options A, B, and D are incorrect as they all require an instance of the outer class. The only option that is correct is C, which states that an object of an inner class can be created without an instance of the outer class if the inner class is declared as static. This means that the inner class is not tied to any specific instance of the outer class and can be accessed independently.