Nearby lessons

46 of 125

Java - Static Nested Class

📌 What You Will Learn
  • What an inner class is and why we need it
  • The four types: member, static nested, local, anonymous
  • How to create objects of inner classes
  • Where anonymous inner classes are used in real projects

Static Nested Class is a core concept of the Java language. This lesson explains Static Nested Class with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Static Nested Class

If we put the keyword static before the inner class, it becomes a static nested class. It does not need an outer object — you can create it directly with the outer class name.

In simple words: A static nested class belongs to the outer class, not to an outer object. Create it directly with Outer.Nested n = new Outer.Nested(); — no outer object is needed.
Example01
JCode Cell
1class Outer {
2 static class Nested {
3 void show() {
4 System.out.println("Static nested class");
5 }
6 }
7}
8 
9class Test {
10 public static void main(String[] args) {
11 Outer.Nested n = new Outer.Nested(); // no outer object needed
12 n.show();
13 }
14}
Output
Static nested class
📝 Key Takeaways
  • Inner class = a class declared inside another class.
  • Member inner class needs an outer object: outer.new Inner().
  • Static nested class is created with Outer.Nested, no outer object needed.
  • Local inner class lives inside a method.
  • Anonymous inner class has no name and is used for one-time implementations.
  • Modern code prefers lambda expressions for one-method interfaces, but anonymous classes still appear in real projects.

🧠 Test Your Knowledge

2 Questions
Progress: 0 / 2