IntelliJ IDEA 2021.3 Help

Move Refactorings

The Copy refactoring lets you create a copy of a class in a different package. It also lets you create a copy of a file, directory or package in a different directory or package.

Perform copy refactoring

  1. Select an identifier that you want to refactor (for example, a class in the editor or a file in the Project tool window).

  2. Select Refactor | Copy F5

  3. In the Copy window, specify the name and location for your copy, and click OK.

The Move refactoring lets you move packages and classes between the source roots of a project, class members to other classes and inner classes to upper hierarchy levels. For example, you can perform the move refactoring on method or field if it is used more in another class than in its own class.

Perform move refactoring

  1. Select an identifier that you want to refactor.

  2. Select Refactor | Move (or press F6).

  3. In the dialog that opens depending on the item you have selected for your refactoring, specify the appropriate options and click Refactor (OK for a package). You can also click Preview, if available, to preview the potential changes.

  4. Specify a path to the target directory, a filename, and a package name.

Move static method to another class

  1. Open your class in the editor, place a caret at the static method you want to move and press F6.

  2. In the Move Members dialog specify the following options:

    • In the Members to be moved to another class (static only) field, select the checkboxes next to the methods that you want to move to another class. The list shows all the static methods detected in the current class.

    • In the Move Member To Class field, type the fully qualified name of the class where you want to move the members selected in the methods' list. You can click the Ellipsis icon to select or search for existing classes.

    Move members dialog

  3. Click Refactor to proceed or Preview to check the results before the actual refactoring.

Move an instance method to another class

You can move an instance (non-static) method to a different class if this method has a type parameter in your project. In any other cases, you would need to make this method static first.

Message on moving
            an instance method without a type parameter
  1. In the editor, position the caret at an instance method and press F6 (Refactor | Move).

  2. In the Move Instance Method dialog that opens, specify the following:

    • In the Select an instance expression list, select the target class to move an instance method to. The list of potential move targets includes the method parameters' classes and fields' classes in the current class.

    • In the Visibility area, select the preferred visibility modifier for the target method.

    • In the Parameter name to replace the "parameter reference" to class <class_name> field, type the desired name of the parameter. When refactoring is performed, the parameter will be added for the method being moved, which will replace all parameter references to the current class.

    Move instance method dialog

  3. Click Refactor to proceed or Preview to check the results before the actual refactoring.

Instance method example

Let's move the getName instance method from the Test class to the Car class.

Before

After

import java.lang.reflect.InvocationTargetException; public class Test { public static void main(String[] args) throws Exception { Car c= new Car(); System.out.println(c.getName(new Test())); } } class Car { String name = "Default Car"; Car() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { } String getName(Test anotherObject){ System.out.print(anotherObject.toString()); return this.name; } }
import java.lang.reflect.InvocationTargetException; public class Test { public static void main(String[] args) throws Exception { Car c= new Car(); System.out.println(new Test().getName(c)); } String getName(Car car){ System.out.print(toString()); return car.name; } } class Car { String name = "Default Car"; Car() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { } }
Last modified: 01 August 2022