Static Inner Classes

Definition:

  • A static nested class is a nested class that is declared static. Because it is static, it cannot directly access instance variables and methods of the outer class.

Key Points:

  • Static nested classes can access the static members of the outer class.
  • They do not have a reference to the outer class's instance, so they cannot access non-static members directly.
  • They are associated with the outer class rather than instances of the outer class.

Use Cases:

  • Static nested classes are often used for utility or helper classes that do not need access to instance variables of the outer class.
  • Can be used for organizing classes that are closely related to the outer class but do not require access to the outer class’s instance data.
Code Example
Output
Static Nested Class: Outer Static Variable