- 1Java OOP Introduction
- 2Java Class
- 3Java Class Constructor
- 4Java Class Objects
- 5Java Access Modifiers
- 6Java Static Variables in Classes
- 7Java Static Methods Explained
- 8Java Static Blocks
- 9Java final Variables
- 10Java final Methods
- 11Java final class
- 12Inheritance in Java
- 13Java Method Overriding
- 14Java Abstraction in OOP
- 15Interfaces in Java
- 16Polymorphism in Java
- 17Encapsulation in Java
- 18Java Nested Classes
- 19Java Nested Static Class
- 20Java Anonymous Class
- 21Java Singleton Class
- 22Java Enums
- 23Reflection in Java
Java Nested Static Class
Examples and Explanation
In 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.
Why Use a Static Nested Class?
- To group classes that belong together logically.
- To improve encapsulation and code organization.
- To access the nested class without creating an instance of the outer class.
Syntax of a Static Nested Class
class OuterClass {
static class NestedStaticClass {
void display() {
System.out.println("Inside static nested class.");
}
}
}
How to Create an Object of a 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.
Explanation
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
.
Accessing Static Members of Outer Class
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
Limitations of Static Nested Classes
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
}
}
}
Nested Static Class with Constructor
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
Real-World Analogy
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).
When Should You Use Static Nested Classes?
- When the nested class is only relevant to its outer class.
- When the nested class doesn't need access to the outer class’s instance variables or methods.
- To enhance code readability and structure.
Quick Recap
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 |
Conclusion
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.
QUIZ
Question 1:Which of the following best describes a Java static nested class?
Question 2:Static nested classes in Java can directly access both static and non-static members of the outer class.
Question 3:Which of the following are valid reasons to use a static nested class in Java?
Question 4:What will be the output of the following Java program?
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();
}
}
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();
}
}