Nested Classes

One aspect of the Java language that is not widely understood is the concept of nested classes. These classes allow you to constrain an entire class within a limited scope of another class or method. The concept of nesting a class within another class or method presents unique issues not found elsewhere in object-oriented programming. Not all types of nested classes should be used routinely, so you will likely encounter most of them in other people's code. Therefore, it is important that you understand how the various nested classes function. Nested classes fall into one of three basic categories:

  • Inner classes
  • Limited-scope inner classes
  • Static nested classes

Each of these categories has its own access rules and usage.