Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Unnesting Classes

As a project grows, you might find that some classes become tangled with a dense structure of inner classes that are hard to read and which cannot be elegantly modified or extended. If this is the case, you might want to simplify the nesting structure and move some classes into their own source files.

The IDE has a pair of useful commands for simplifying your code's nesting structure:

  • Move Inner to Outer Level. This command moves a class up one level.

    If the class is a top-level inner class, it is made an outer class and moved into its own source file.

    If the class is nested within an inner class, method, or variable scope, it is moved up one level.

  • Convert Anonymous Class to Inner. This command converts an anonymous inner class (i.e., a class that is unnamed and has no constructor) into a named inner class (inner class that has a name and a constructor). This also makes it possible for other code to reference this class.

Moving an Inner Class up One Level

To move an inner class up one level:

  1. In the Source Editor, right-click the inner class that you want to move and choose Refactor | Move Inner to Outer Level.
  2. In the Class Name field of the Move Inner to Outer Level dialog box, set the name of the class.
  3. Select the Declare Field for the Current Outer Class checkbox if you want to generate a field in the moved inner class to hold the outer class instance and include a reference to that instance as a parameter in the moved class' constructor.

    If you select this option, fill in the Field Name text field with a name for the the outer class' instance field.

  4. Click Next.

    If you have deselected the Preview All Changes checkbox, the changes are applied immediately.

    If you leave the Preview All Changes checkbox selected, the Refactoring window appears with a preview of the changes.

  5. In the Refactoring window, look at the preview of the code to be changed. If there is a modification that you do not want to be made, deselect the checkbox next to the line for that change.
  6. Click Do Refactoring.

    If you later find that the refactoring has had some consequences that you would like to reverse, you can choose Refactor | Undo.

    Unless you have selected the Preview All Changes box, the inner class is immediately moved up one level.

    If you have selected the Preview All Changes box, the changes to be made are shown in the Refactoring window. You can then apply the changes by clicking Do Refactoring.

    If the result is different from what you expected, you can reverse the command by choosing Refactor | Undo.

Converting an Anonymous Inner Class to a Named Inner Class

To convert an anonymous inner class to a named inner class:

  1. In the Source Editor, right-click the anonymous inner class that you want to convert and choose Refactor | Convert Anonymous Class to Inner.
  2. In the Inner Class Name field of the Convert Anonymous Class to Inner dialog box, enter a name for the class.
  3. In the Access field, select the access modifier for the class.
  4. Select the Declare Static checkbox if you want the class to be static.
  5. In the Constructor Parameters list, use the Move Up and Move Down buttons to set the order of the parameters.
  6. Click Next.

Unless you have selected the Preview All Changes box, the anonymous class is converted to the named inner class.

If you have selected the Preview All Changes box, the changes to be made are shown in the Refactoring window. You can then apply the changes by clicking Do Refactoring.

If the result is different from than expected, you can reverse the command by choosing Refactor | Undo.

  • + Share This
  • 🔖 Save To Your Account