Java Inner Classes
In this page:
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?
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();
}
}
Login to try C/C++/Java/PHP code in the editor
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
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();
}
}
Login to try C/C++/Java/PHP code in the editor
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
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();
}
}
Login to try C/C++/Java/PHP code in the editor
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
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();
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: