⬅ Previous Topic
Java Nested ClassesNext Topic ⮕
Java Anonymous Class⬅ Previous Topic
Java Nested ClassesNext Topic ⮕
Java Anonymous ClassIn Java, classes can be defined inside other classes — these are called nested classes. When a nested class is marked as static
, it becomes a nested static class. This concept might seem a little strange at first, but it's extremely useful when a class logically belongs to another class, yet does not require access to the outer class's instance variables or methods.
class OuterClass {
static class NestedStaticClass {
void display() {
System.out.println("Inside static nested class.");
}
}
}
Because the nested class is static, you don’t need an instance of the outer class to create its object. Here's how you do it:
public class Test {
public static void main(String[] args) {
OuterClass.NestedStaticClass obj = new OuterClass.NestedStaticClass();
obj.display();
}
}
Inside static nested class.
We define NestedStaticClass
inside OuterClass
and mark it as static
. In main()
, we directly access the static nested class using the syntax OuterClass.NestedStaticClass
without needing an object of OuterClass
.
A static nested class can access all static members (variables and methods) of its outer class:
class Outer {
static int outerValue = 42;
static class StaticInner {
void show() {
System.out.println("Outer static value: " + outerValue);
}
}
}
public class Demo {
public static void main(String[] args) {
Outer.StaticInner obj = new Outer.StaticInner();
obj.show();
}
}
Outer static value: 42
Static nested classes cannot access non-static members of the outer class directly. If you try to do so, you’ll get a compilation error. Here’s an example:
class Outer {
int outerInstanceVar = 5;
static class StaticInner {
void show() {
// System.out.println(outerInstanceVar); // ❌ Error: Cannot access non-static variable
}
}
}
Just like any other class, you can add constructors to static nested classes:
class Book {
static class Publisher {
String name;
Publisher(String name) {
this.name = name;
}
void print() {
System.out.println("Published by: " + name);
}
}
}
public class DemoPublisher {
public static void main(String[] args) {
Book.Publisher p = new Book.Publisher("TutorialKart Publishing");
p.print();
}
}
Published by: TutorialKart Publishing
Imagine you’re designing a Car class. You might define a static nested class called EngineSpecs
inside Car
because it logically belongs to Car
but doesn't need direct access to car's instance-level properties (like color or mileage).
Feature | Static Nested Class |
---|---|
Access outer class static members | ✅ Yes |
Access outer class non-static members | ❌ No |
Instance needed to use | ❌ No outer class instance required |
Use case | Helper class tightly coupled to outer class |
The static nested class in Java is a neat tool to structure your code better, especially when dealing with logically grouped elements. While it can’t access instance variables of the outer class, its ability to operate independently makes it a clean and encapsulated design choice.
class Outer {
static class Inner {
void show() {
System.out.println("Hello from static nested class");
}
}
}
public class Main {
public static void main(String[] args) {
Outer.Inner obj = new Outer.Inner();
obj.show();
}
}
⬅ Previous Topic
Java Nested ClassesNext Topic ⮕
Java Anonymous ClassYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.