IntelliJ IDEA 2024.1 Help

Move and copy 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. In the main menu, go to Refactor | Copy or press F5.

  3. In the Copy dialog, 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 a method or a 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. In the main menu, go to 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. For more information about the dialogs, refer to the relevant article in the Move Dialogs section.

  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 the caret at the static method you want to move and press F6 (Refactor | Move).

    The Move Static Members dialog opens.

  2. In the To (fully qualified name) 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.

  3. In the Members to be moved (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.

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

Move members dialog

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, place the caret at an instance method and press F6 (Refactor | Move).

    The Move Instance Method dialog opens.

  2. From 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.

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

  4. In the Select a name for 'parameter reference' parameter 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.

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

Move instance method dialog

Instance method example

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

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 { } }
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; } }
Last modified: 28 June 2024