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 a private inner class be accessed outside of its enclosing class?

  1. Yes, using reflection

  2. No

  3. Yes, if a public method returns its reference

  4. Yes, by inheriting the outer class

The correct answer is: No

Private inner classes are not accessible outside of their enclosing class because they have restricted access modifiers. Option A is incorrect because although reflection can allow access to private members, it cannot bypass the access modifier set on the inner class. Option C is incorrect because a public method within the enclosing class would not be able to access the private inner class. Option D is incorrect because inheriting the outer class does not automatically grant access to the private inner class.