Interview Q&A · Java · 5 questions in this topic
Member Inner Classes — Java Interview Q&A
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
- 1Declared inside the outer class body without static, like an instance field.
- 2Sees all outer members, including private ones.
- 3Can carry any access modifier and can be abstract or final.
- 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
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
- 1Inner classes carry an implicit outer reference; static nested do not.
- 2Static nested classes can declare static members; inner classes cannot.
- 3An inner instance keeps the outer object reachable.
- 4Prefer static nested unless the outer state is genuinely needed.
Example
Outer.Nested n = new Outer.Nested(); // static nested Outer o = new Outer(); Outer.Inner i = o.new Inner(); // inner
Can an inner class use local variables of the enclosing method?
Answer
Only effectively final ones. A local or anonymous class may capture a local variable of the enclosing method if that variable is never reassigned after initialisation; the compiler copies the value into the class object, which is why mutation is forbidden. Since Java 8 the final keyword is no longer required, but the effectively-final rule is unchanged. Fields of the enclosing instance are always accessible because they live on the outer object.
Key points
- 1Captured locals must be final or effectively final.
- 2The value is copied, not shared by reference.
- 3Outer instance fields are not captured; they are read through the outer reference.
- 4A one-element array or AtomicInteger is the usual workaround when mutation is required.
Example
void run() { int limit = 10; // effectively final Runnable r = () -> System.out.println(limit); // limit = 20; // compile error: captured variable would change }
How do you create an inner class instance from outside the outer class?
Answer
You need an outer instance first: outer.new Inner(). The syntax exists because the inner object binds to that specific outer object, and a different outer instance would produce a different inner class instance with a different view of the outer fields. Static nested classes and local classes declared in a static method are created normally with new, since they have no outer instance to bind to.
Key points
- 1Syntax is outer.new Inner(), not new Outer.Inner().
- 2Each inner instance binds to one outer instance.
- 3Static nested classes use new Outer.Nested().
- 4Inside the outer class the short form new Inner() is allowed.
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission