Interview Q&A · Java · 6 questions in this topic
Nested and Inner Classes — Java Interview Q&A
How Java nests one class inside another, and when to choose a static nested class versus an inner class. Part of Nested & Inner Classes · 294 questions in the Java bank.
What are nested classes?
Answer
A nested class is a class declared inside another class so the two stay logically together. Java divides nested classes into static nested classes and inner (non-static nested) classes. The static form needs no outer instance and is mainly namespacing plus encapsulation, while the inner form is bound to an outer instance and can use its state.
Key points
- 1A nested class is a member of its outer class, just like a field or method.
- 2Static nested class: declared with static, usable without an outer object.
- 3Inner class: no static modifier, tied to one outer instance.
- 4Nesting improves encapsulation by hiding helpers that no other class needs.
What are inner (non-static nested) classes?
Answer
An inner class is a nested class without the static modifier, so every instance belongs to an instance of the outer class. Because of that link it can directly read the outer object fields and methods, including private ones. Java has three kinds of inner class: member inner classes, local inner classes, and anonymous inner classes.
Key points
- 1Each inner object holds a hidden reference to its outer object.
- 2Member inner class: declared at class level without static.
- 3Local inner class: declared inside a method or block.
- 4Anonymous inner class: declared and instantiated in one expression with no name.
Why use nested classes?
Answer
Nested classes group a helper type with the only class that uses it instead of polluting the package with a tiny top-level class. They strengthen encapsulation because an inner class can use private outer members while staying hidden from the rest of the program. They also improve readability by placing related code together, and they let you hide an implementation such as a button event handler behind the class it serves.
Key points
- 1Grouping: keeps a one-use helper next to the class that needs it.
- 2Encapsulation: inner code can touch private outer state without exposing it.
- 3Readability: less navigation between tiny top-level files.
- 4Hiding: event handlers and iterators can stay private implementation details.
What are static nested classes?
Answer
A static nested class is a nested class declared with the static modifier, so it does not hold a reference to an outer instance. It can be created and used without first creating the outer object, but for the same reason it cannot directly touch the outer instance fields or instance methods. A well known example is java.util.Map.Entry, which describes a map entry without needing a map instance.
Key points
- 1Declared with static inside the outer class; not technically called an inner class.
- 2Cannot access outer instance members, only static members of the outer class.
- 3Behaves like a top-level class with extra namespacing and access rights.
How are static nested classes instantiated?
Answer
You qualify the nested name with the outer class name and call new on it directly, with no outer object involved. This works from any code that can see the nested class, subject to its access modifier. Because no outer instance exists, the nested class can only use static members of the outer class on its own.
Example
Outer.Inner x = new Outer.Inner(); // no Outer object needed x.display();
💡 If you need an outer object to create it, it is an inner class, not a static nested class.
What are the features of a local inner class?
Answer
A local inner class is defined inside a block, and it cannot have an access modifier because its visibility is already limited to that block. It cannot declare static members, though it may itself be declared abstract or final like a normal class. It can be defined inside loops and if blocks as well as methods, and it can only use local variables that are final or effectively final.
Key points
- 1No access modifier allowed; visibility ends at the enclosing block.
- 2No static declarations inside, apart from compile-time constants.
- 3May be abstract or final, and may extend a class or implement an interface.
- 4Captured locals must be final or effectively final since Java 8.
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission