Classes (especially complex ones) that are nested multiple levels deep can be difficult to understand because they have access to variables from all of the classes that enclose them. Such classes can also be difficult to unit test. Specific exceptions are made for:

The solution is to move one or more of the nested classes into a higher scope, less deeply-nested (see example below). When you move a nested class, you must:

In the following example Z1 is difficult to read because it is deeply nested.

In this example, there are no nested classes and you can clearly see which variables affect which class.

  • The Java Tutorials: Nested Classes.