- Opening the Source Editor
- Managing Automatic Insertion of Closing Characters
- Displaying Line Numbers
- Generating Code Snippets without Leaving the Keyboard
- Using Code Completion
- Inserting Snippets from Code Templates
- Using Editor Hints to Generate Missing Code
- Matching Other Words in a File
- Generating Methods to Implement and Override
- Generating JavaBeans Component Code
- Creating and Using Macros
- Creating and Customizing File Templates
- Handling Imports
- Displaying Javadoc Documentation While Editing
- Formatting Code
- Text Selection Shortcuts
- Navigating within the Current Java File
- Navigating from the Source Editor
- Searching and Replacing
- Deleting Code Safely
- Changing a Method's Signature
- Encapsulating a Field
- Moving a Class to a Different Package
- Moving Class Members to Other Classes
- Creating a Method from Existing Statements
- Creating an Interface from Existing Methods
- Extracting a Superclass to Consolidate Common Methods
- Changing References to Use a Supertype
- Unnesting Classes
- Tracking Notes to Yourself in Your Code
- Comparing Differences Between Two Files
- Splitting the Source Editor
- Maximizing Space for the Source Editor
- Changing Source Editor Keyboard Shortcuts
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:
- In the Source Editor, right-click the inner class that you want to move and choose Refactor | Move Inner to Outer Level.
- In the Class Name field of the Move Inner to Outer Level dialog box, set the name of the class.
-
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.
-
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.
- 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.
-
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:
- In the Source Editor, right-click the anonymous inner class that you want to convert and choose Refactor | Convert Anonymous Class to Inner.
- In the Inner Class Name field of the Convert Anonymous Class to Inner dialog box, enter a name for the class.
- In the Access field, select the access modifier for the class.
- Select the Declare Static checkbox if you want the class to be static.
- In the Constructor Parameters list, use the Move Up and Move Down buttons to set the order of the parameters.
- 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.