- 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
Moving Class Members to Other Classes
The IDE has the Pull Up and Push Down refactoring commands for moving methods and fields to other classes and interfaces. When you use these commands to move class members, the IDE updates references to those members throughout your project. These commands are useful for improving the inheritance structure of your code. You can:
- Move methods and fields to a superclass or super-interface.
- Leave method implementations in the current class but create abstract declarations for those methods in a superclass.
- Move methods and fields to the class's subclasses or sub-interfaces.
- Move the implementations of methods to subclasses while leaving abstract method declarations in the current class.
- Move the interface name from the implements clause of a class to the implements clause of another class.
Moving Code to a Superclass
To move a member, a method's declaration, or an implements clause from the current class to a superclass:
- In the Source Editor or the Projects window, select the class or interface that contains the member or members that you want to move.
- Choose Refactor | Pull Up to open the Pull Up dialog box.
- In the Destination Supertype drop-down list, select the superclass or interface that you want to move the members to.
-
Select the checkbox for each member that you want to move.
If you want to leave the method implementation in the current class and create an abstract declaration for the method in the superclass, select the Make Abstract checkbox for the method.
If the class from which you are moving members implements an interface, a checkbox for that interface is included in the dialog box. If you select the checkbox for that interface, the interface is removed from the implements clause of the current class and moved to the implements clause of the class to which you are moving members. (If you select a checkbox for an interface, be sure that all the checkboxes for the methods declared in that interface are also selected.)
-
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.
Moving Code to Subclasses
To move a member, a method's implementation, or an implements clause from the current class to that class' subclasses:
- In the Source Editor or the Projects window, select the class or interface that contains the member or members that you want to move.
- Choose Refactor | Push Down to open the Push Down dialog box.
-
Select the checkbox for each member you want to move.
If you want to leave an abstract declaration for the method in the current class and move the implementation to the subclasses, select the Keep Abstract checkbox for the method.
If the class from which you are moving members implements an interface, a checkbox for that interface is included in the dialog box. If you select the checkbox for that interface, the interface is removed from the implements clause of the current class and moved to the implements clause of the class to which you are moving members.
-
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.