Interview Q&A · Java · 5 questions in this topic

Member Inner Classes — Java Interview Q&A

All Java topics

Inner classes declared as members of the outer class and how to create them from an outer instance. Part of Nested & Inner Classes · 294 questions in the Java bank.

What are member inner classes?

Answer

A member inner class is a non-static class declared at member level inside its outer class. It can read every member of the outer object, including private fields, and it can itself use public, private, protected, or package-private access plus abstract or final. Like other inner classes, it cannot declare static members apart from constants because it belongs to an outer instance rather than to the class as a whole.

Key points

  1. 1Declared inside the outer class body without static, like an instance field.
  2. 2Sees all outer members, including private ones.
  3. 3Can carry any access modifier and can be abstract or final.
  4. 4Cannot hold static declarations except compile-time constants.

How are member inner classes instantiated?

Answer

A member inner class needs an existing outer instance because every inner object is attached to one. From outside the outer class you write outerRef.new Inner() to create it, while code inside the outer class can simply call new Inner(). Trying to create one without an outer instance is a compile error.

Example

Example.javajava
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
inner.display();

💡 Inside the outer class, write new Inner(); outside, you must qualify with an outer reference.

What is the difference between a static nested class and an inner class?

Answer

A static nested class is declared with static and behaves like a top-level class that happens to be namespaced: it holds no reference to an outer instance and is created with new Outer.Nested(). An inner (non-static member) class keeps an implicit reference to the enclosing instance, so it can read the outer object’s fields and is created with outer.new Inner(). That hidden reference is why inner classes cannot be created without an outer object and why they can keep the outer instance alive.

Key points

  1. 1Inner classes carry an implicit outer reference; static nested do not.
  2. 2Static nested classes can declare static members; inner classes cannot.
  3. 3An inner instance keeps the outer object reachable.
  4. 4Prefer static nested unless the outer state is genuinely needed.

Example

Example.javajava
Outer.Nested n = new Outer.Nested();     // static nested
Outer o = new Outer();
Outer.Inner i = o.new Inner();           // inner

Learn this topic

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits