🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Nested and Anonymous Classes: all 10 questions

Quiz: Nested and Anonymous Classes · Question 6 of 10

Why is a static nested class usually preferred when the nested class does not use outer fields?

Why is a static nested class usually preferred when the nested class does not use outer fields?

  1. A.Static classes run faster
  2. B.Inner classes cannot have methods
  3. C.It avoids a hidden reference to the outer object, which costs memory and can keep the outer object alive
  4. D.Static nested classes can be public; inner classes cannot
Answer & explanation

Correct answer: It avoids a hidden reference to the outer object, which costs memory and can keep the outer object alive

An inner object always holds Outer.this, even if it never uses it.

Can you get the rest right without peeking?

Take the timed Quiz: Nested and Anonymous Classes quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →