← Back to Core Java Course | Chapter 9: OOP Advanced | Lesson 7 of 8

Java Inner Classes

What is an Inner Class?

An inner class is defined entirely inside another (outer) class and typically exists to serve that outer class's needs closely — it's a way to logically group a small helper class with the one larger class that actually uses it.

Example: What is an Inner Class?

java
class Outer {
	class Inner {
		void show() {
			System.out.println("Inside inner class");
		}
	}
}
public class Main {
	public static void main(String[] args) {
		Outer outer = new Outer();
		Outer.Inner inner = outer.new Inner();
		inner.show();
	}
}

Instantiating Inner Classes

Creating an instance of a regular (non-static) inner class requires an existing outer-class instance first, since the inner object is implicitly tied to it — the syntax looks like outerInstance.new InnerClass().

Example: Instantiating Inner Classes

java
class Outer {
	class Inner {
	}
}
public class Main {
	public static void main(String[] args) {
		Outer outer = new Outer();
		Outer.Inner inner = outer.new Inner(); // requires an outer instance first
		System.out.println(inner);
	}
}

Accessing Outer Class Members

A non-static inner class can directly access every member of its enclosing outer class, private fields and methods included, without any special access modifiers or getter/setter workaround — this tight coupling is exactly what makes inner classes convenient for closely related helper logic.

Example: Accessing Outer Class Members

java
class Outer {
	private int value = 42;
	class Inner {
		void show() {
			System.out.println(value); // reaches outer's private field directly
		}
	}
}
public class Main {
	public static void main(String[] args) {
		Outer outer = new Outer();
		outer.new Inner().show();
	}
}

Static Nested Classes

A static nested class, declared with the static keyword, does not hold an implicit link to any outer-class instance, so you can create one on its own (new Outer.StaticNested()) without first creating an Outer object at all.

Example: Static Nested Classes

java
class Outer {
	static class Nested {
		void show() {
			System.out.println("Static nested class");
		}
	}
}
public class Main {
	public static void main(String[] args) {
		Outer.Nested nested = new Outer.Nested(); // no outer instance needed
		nested.show();
	}
}

Local Inner Classes

A local inner class is declared inside a method body rather than at the class level, and its scope is limited to that method — it's a niche tool for a helper class that's only ever needed inside one specific piece of logic.

Example: Local Inner Classes

java
public class Main {
	static void method() {
		class Helper { // scoped to this method only
			void show() {
				System.out.println("Local inner class");
			}
		}
		new Helper().show();
	}
	public static void main(String[] args) {
		method();
	}
}
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.