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

> **Tip:**
> You can access all refactoring actions through the context menu.

Procedure: 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` (Windows), `F5` (macOS), `F5` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ Y` (macOS System Shortcuts), `F5` (XWin), `F5` (GNOME), `F5` (KDE), `F5` (Emacs), `F5` (Sublime Text), `F5` (Sublime Text (macOS)), `F5` (NetBeans), `F5` (Visual Studio), `F5` (Visual Studio (macOS)), `F5` (Eclipse), `F5` (Eclipse (macOS)).

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.

Procedure: Perform move refactoring

1. Select an identifier that you want to refactor.

2. In the main menu, go to `Refactor | Move` or press `F6` (Windows), `F6` (macOS), `F6` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ I` (macOS System Shortcuts), `F6` (XWin), `F6` (GNOME), `F6` (KDE), `F6` (Emacs), `F6` (Sublime Text), `F6` (Sublime Text (macOS)), `Ctrl+M` (NetBeans), `Ctrl+R, O` (Visual Studio), `⌘ R, O` (Visual Studio (macOS)), `Alt+Shift+V` (Eclipse), `⌘ ⌥ V` (Eclipse (macOS)).

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](move-dialogs.html) section.

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

> **Note:**
> When you perform the move refactoring for a package, you need to specify where to move the package and its contents (to another package, source root, or another directory).

> **Tip:**
> To undo the refactoring, press `Ctrl+Z` (Windows), `⌘ Z` (macOS), `⌘ Z` (IntelliJ IDEA Classic (macOS)), `⌘ Z` (macOS System Shortcuts), `Ctrl+Z` (XWin), `Ctrl+Z` (GNOME), `Ctrl+Z` (KDE), `Ctrl+Shift+Minus` (Emacs), `Ctrl+Z` (Sublime Text), `⌘ Z` (Sublime Text (macOS)), `Ctrl+Z` (NetBeans), `Ctrl+Z` (Visual Studio), `⌘ Z` (Visual Studio (macOS)), `Ctrl+Z` (Eclipse), `⌘ Z` (Eclipse (macOS)).

> **Tip:**
> The Move refactoring is also available from [UML Class diagram](class-diagram.html).

Procedure: 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` (Windows), `F6` (macOS), `F6` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ I` (macOS System Shortcuts), `F6` (XWin), `F6` (GNOME), `F6` (KDE), `F6` (Emacs), `F6` (Sublime Text), `F6` (Sublime Text (macOS)), `Ctrl+M` (NetBeans), `Ctrl+R, O` (Visual Studio), `⌘ R, O` (Visual Studio (macOS)), `Alt+Shift+V` (Eclipse), `⌘ ⌥ V` (Eclipse (macOS)) (`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](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.general.ellipsis.svg) 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](https://resources.jetbrains.com.cn/help/img/idea/2026.2/move_static_members.png)

Procedure: 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](https://resources.jetbrains.com.cn/help/img/idea/2026.2/move_instance_method_message.png)

1. In the editor, place the caret at an instance method and press `F6` (Windows), `F6` (macOS), `F6` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ I` (macOS System Shortcuts), `F6` (XWin), `F6` (GNOME), `F6` (KDE), `F6` (Emacs), `F6` (Sublime Text), `F6` (Sublime Text (macOS)), `Ctrl+M` (NetBeans), `Ctrl+R, O` (Visual Studio), `⌘ R, O` (Visual Studio (macOS)), `Alt+Shift+V` (Eclipse), `⌘ ⌥ V` (Eclipse (macOS)) (`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](https://resources.jetbrains.com.cn/help/img/idea/2026.2/Move_instance_method_dialog.png)

## Instance method example

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

Before:

```JAVA
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 {
    }
}
```

After:

```JAVA
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;
    }
}
```

## See also

### Getting Started

[Select code constructs](working-with-source-code.html#editor_code_selection)

### Reference

[Move dialogs](move-dialogs.html)  [Move Namespace Dialog](move-namespace-dialog.html)

### External Links

[https://blog.jetbrains.com/webide/2013/03/refactoring-for-classes-in-phpstorm-6-move-class/](https://blog.jetbrains.com/webide/2013/03/refactoring-for-classes-in-phpstorm-6-move-class/)

