Types of Nested Classes

Types of Nested Classes Java supports four types of nested classes. Each type serves different use cases and has unique access rules. 1. Static Nested Class Defined using the static keyword inside another class. Can access only static members of the outer class. Does not require an instance of the outer class to be instantiated. class Outer { static int value = 10; static class StaticNested { void show() { System.out.println("Value: " + value); } } } Usage: ...

May 8, 2025 · 2 min · Rohan

Nested Classes

Nested Classes A Nested Class is a class that is defined within another class. In Java, nested classes can be categorized into several types, such as static nested classes, inner classes, local classes, and anonymous classes. Types of Nested Classes: Static Nested Class: A nested class that is declared static. It can access the static members of the outer class but cannot access the non-static members. Example: class Outer { static int num = 10; static class Inner { void display() { System.out.println(num); } } } 2. **Inner Class (Non-static Nested Class)**: A class that is defined inside another class without the `static` keyword. It can access both static and non-static members of the outer class. * Example: ```java class Outer { int num = 10; class Inner { void display() { System.out.println(num); } } } Local Inner Class: A class defined inside a method. It can access the local variables and parameters of the method. ...

May 8, 2025 · 2 min · Rohan