Java Notes
Learn inner classes in Java — member inner class, static nested class, local inner class, accessing outer class members, and when to use nested classes.
An inner class is a class defined inside another class. Java supports this when a class makes no sense on its own — it only exists to serve the outer class.
There are three main types. Each has a specific purpose.
Member Inner Class (Non-Static)
A member inner class is declared inside an outer class but outside any method. It has access to all members (including private) of the outer class.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
class Innerdeclared insideclass Outer— Inner is a member of Outer.outerValuein Inner — even though it isprivatein Outer, Inner can access it directly. Inner has full access to Outer.outer.new Inner()— to create an Inner object, you must first have an Outer object. The inner object is tied to the outer.
Static Nested Class
A static nested class is declared with static. It does NOT have access to instance members of the outer class. It is more independent — you can create it without creating the outer class.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
static class Processor— static nested class. No implicit link to an outer object.new Computer.Processor(...)— created directly without needing aComputerinstance.- Cannot access
brand(outer instance field) — because there is no outer instance.
Use static nested classes when the nested class is logically connected to the outer class but does not need outer instance data. Common example: Map.Entry in Java's HashMap.
Local Inner Class
A local inner class is declared inside a method. It only exists for that method.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
UpperCasePrinter is only visible inside formatAndPrint(). Outside the method, it does not exist. This is useful when a small helper is only needed for one specific task.
Real-World Use Case — Builder Pattern
A common use of static nested classes in real Java code is the Builder pattern:
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Pizza.Builder is a static nested class. It builds a Pizza step by step. The Pizza constructor is private — the only way to create one is through the Builder. This is a classic Java design pattern.
Member Inner vs Static Nested — Quick Comparison
| Member Inner Class | Static Nested Class | |
|---|---|---|
| Access to outer instance fields | Yes | No |
| Needs outer object to create | Yes | No |
| Memory | Holds reference to outer | No reference to outer |
| Use when | Tightly coupled to outer state | Logically grouped but independent |
Common Mistake 1: Creating Inner Without Outer
// WRONG — cannot create member inner class without outer instance
Inner inner = new Inner(); // COMPILE ERROR
// CORRECT
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();Common Mistake 2: Accessing Outer Instance Members in Static Nested
public class Outer {
int x = 5;
static class StaticNested {
void show() {
System.out.println(x); // COMPILE ERROR — x is instance field
}
}
}Static nested class has no this for the outer class. It cannot access instance fields.
Practice Task
Task: Create a class Library with a private field libraryName. Inside it, create a member inner class Book with fields title and author and a method display() that prints "[title] by [author] — available at [libraryName]". In main, create a Library object, then create two Book objects inside it and call display() on each.
Hint: Book accesses libraryName directly from Library. Create books using library.new Book().
Solution:
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
What to observe: b1.display() prints "City Library" even though Book never stores libraryName. It directly reads the outer class's private field. Change lib's name (if you add a setter) — all books under it automatically show the new name.
Continue the lesson path