Home > Articles > Programming > Java

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

Moving a Class to a Different Package

If you want to place a class in a different package, you can use the IDE's refactoring features to move the class and then update references to that class automatically throughout your project.

To move a class:

  1. In the Projects window, drag the class from its current package to the package you want to place it in. (You can also use the Cut and Paste commands in the contextual menus or the corresponding keyboard shortcuts.)
  2. In the Move Class or Move Classes dialog box (shown in Figure 5-16), click Next after verifying that the To Package and This Class fields reflect the destination package and the class you are moving. (If you move multiple classes, a List of Classes text area is shown instead of the This Class field.)

    keegan_fig05_16.jpg

    Figure 5-16 Move Classes dialog box

    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.

  3. In the Refactoring window (shown in Figure 5-17), 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.

    keegan_fig05_17.jpg

    Figure 5-17 Refactoring window

  4. 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.

  • + Share This
  • 🔖 Save To Your Account