Member Inner Classes ( Non-static Inner Classes )

A non-static inner class, often just called an inner class, is a class that is defined inside another class and is associated with an instance of the outer class.

Key Points:

  • Inner classes can access both static and non-static members of the outer class.
  • They have a reference to the outer class's instance, allowing them to interact with the outer class's instance variables and methods.
  • Inner classes are typically used when you want to encapsulate helper classes or functionality that is closely related to the outer class.

Use Cases:

  • Non-static inner classes are useful when the inner class requires access to the outer class's instance variables or methods.
  • They are commonly used in implementing data structures like linked lists, trees, and in GUI applications where event listeners are implemented as inner classes.
Code Example
Output
Inner Class: Outer Instance Variable