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

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

Prepare for your Java exam with our ultimate quiz based on 'Thinking in Java'. Engage with expertly crafted questions that enhance your learning process. Perfect for Java enthusiasts looking to solidify their knowledge!

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.


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.