# メソッドの抽出

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com/ja-jp/help/idea/llms.txt).

> **TL;DR**
> `リファクタリング | 抽出 / 導入 | メソッド`
>
>
>
> `Ctrl+Alt+M` (Windows), `⌘ ⌥ M` (macOS), `⌘ ⌥ M` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ M` (macOS System Shortcuts), `Ctrl+Alt+M` (XWin), `Ctrl+Alt+M` (GNOME), `Ctrl+Alt+M` (KDE), `Ctrl+Alt+M` (Emacs), `Ctrl+Alt+M` (Sublime Text), `⌘ ⌥ M` (Sublime Text (macOS)), `Ctrl+R, M` (Visual Studio), `⌘ R, M` (Visual Studio (macOS)), `Alt+Shift+M` (NetBeans), `Alt+Shift+M` (Eclipse), `⌘ ⌥ M` (Eclipse (macOS))

メソッドの抽出   リファクタリングを使用すると、グループ化できるコードフラグメントを取得し、別のメソッドに移動して、古いコードをメソッドの呼び出しに置き換えることができます。

> **Note:**
> このリファクタリングは、エディターで [インテンションアクション](intention-actions.html)としても利用できます。 抽出したいコードを選択し、 `Alt+Enter` (Windows), `⌥ ⏎` (macOS), `⌥ ⏎` (IntelliJ IDEA Classic (macOS)), `⌥ ⏎` (macOS System Shortcuts), `Alt+Enter` (XWin), `Alt+Enter` (GNOME), `Alt+Enter` (KDE), `Alt+Enter` (Emacs), `Alt+Enter` (Sublime Text), `⌥ ⏎` (Sublime Text (macOS)), `Alt+Enter` (Visual Studio), `⌥ ⏎` (Visual Studio (macOS)), `Alt+Enter` (NetBeans), `Ctrl+1` (Eclipse), `⌘ 1` (Eclipse (macOS)) を押して   メソッドの抽出   を選択します。

メソッドを抽出する際には、変数を確認する必要があります。 出力変数が 1 つしかない場合、抽出されたメソッドの戻り値として使用されます。 出力変数が複数ある場合、  メソッドの抽出 リファクタリングが適用されず、エラーメッセージが表示されることがあります。

この場合、  メソッドの抽出 を動作させるためのいくつかの回避策があります。 例: すべての出力値を含む特別な data-class を導入することができます。

> **Note:**
> メソッドの抽出リファクタリングには、次の制限があります:
>
>
>
> * ジェネリクス型への参照がある場合、リファクタリングは機能しません。
>
> * 自動モードでは、リファクタリングは複数の出力値では機能しません。 リファクタリングを適用する前にコードを変更する必要があります。

Procedure: メソッドの抽出

1. メソッドに抽出するコードフラグメントを選択します。

2. `Ctrl+Alt+M` (Windows), `⌘ ⌥ M` (macOS), `⌘ ⌥ M` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ M` (macOS System Shortcuts), `Ctrl+Alt+M` (XWin), `Ctrl+Alt+M` (GNOME), `Ctrl+Alt+M` (KDE), `Ctrl+Alt+M` (Emacs), `Ctrl+Alt+M` (Sublime Text), `⌘ ⌥ M` (Sublime Text (macOS)), `Ctrl+R, M` (Visual Studio), `⌘ R, M` (Visual Studio (macOS)), `Alt+Shift+M` (NetBeans), `Alt+Shift+M` (Eclipse), `⌘ ⌥ M` (Eclipse (macOS)) を押すか、メインメニューで  `リファクタリング | 抽出 | メソッド`  に移動します。

または、コード箇所を選択したときに表示される [フローティングツールバー](working-with-source-code.html#floating_toolbar)で、   抽出  をクリックし、   メソッド  を選択します。

3. IntelliJ IDEA が部分的にのみ重複しているコードを検出した場合、リファクタリングを続行するためにパラメーターを抽出することを提案します。

![部分的な重複](https://resources.jetbrains.com/help/img/idea/2026.3/partial_duplicates.png)

> **Tip:**
> メソッドの抽出 リファクタリングを元に戻すには、 `Ctrl+Alt+N` (Windows), `⌘ ⌥ N` (macOS), `⌘ ⌥ N` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ N` (macOS System Shortcuts), `Ctrl+Alt+N` (XWin), `Ctrl+Alt+N` (GNOME), `Ctrl+Alt+N` (KDE), `Ctrl+Alt+N` (Emacs), `Ctrl+Alt+N` (Sublime Text), `⌘ ⌥ N` (Sublime Text (macOS)), `Ctrl+R, I` (Visual Studio), `⌘ R, I` (Visual Studio (macOS)), `Ctrl+Alt+N` (NetBeans), `Alt+Shift+I` (Eclipse), `⌘ ⌥ I` (Eclipse (macOS)) を押して [インライン化リファクタリング](inline.html)を呼び出します。

> **Tip:**
> デフォルトでは、この抽出リファクタリングはエディターで適用されます。 [モーダル経由](extract-method-dialog.html)でリファクタリングを適用するように設定を変更するには、   設定   ダイアログ ( `Ctrl+Alt+S` (Windows), `⌘ Comma` (macOS), `⌘ Comma` (IntelliJ IDEA Classic (macOS)), `⌘ Comma` (macOS System Shortcuts), `Ctrl+Alt+S` (XWin), `Ctrl+Alt+S` (GNOME), `Ctrl+Alt+S` (KDE), `Ctrl+Alt+S` (Emacs), `Ctrl+Alt+S` (Sublime Text), `⌘ Comma` (Sublime Text (macOS)), `Ctrl+Alt+S` (Visual Studio), `⌘ Comma` (Visual Studio (macOS)), `Ctrl+Alt+S` (NetBeans), `Ctrl+Alt+S` (Eclipse), `⌘ Comma` (Eclipse (macOS)) ) を開き、   エディター | コード編集   に移動して、   リファクタリング   領域で   モーダルダイアログ内の   を選択します。

## サンプル

`a+b`  の式をメソッド（Kotlin の関数）に抽出し、重複を置き換えましょう。

Java:

|  前  |  後  |
| --- | --- |
|     ```JAVA public void method() {     int a=1;     int b=2;     int c=a+b;     int d=a+c; }                              ```    |     ```JAVA public void method() {     int a=1;     int b=2;     int c=add(a,b);     int d=add(a,c); } ... private int add(int a, int b) {     return a+b; }                              ```    |

Kotlin:

|  前  |  後  |
| --- | --- |
|     ```KOTLIN fun method(){     val a = 1     val b = 2     val c = a + b     val d = a + b }                              ```    |     ```KOTLIN fun method(){     val a = 1     val b = 2     val c = add(a, b)     val d = add(a, b) } private fun add(a: Int, b: Int) = a + b                              ```    |

## Java レコードを使用してメソッドを抽出する

Java 16 バージョンから、Java レコードを使用してメソッドを抽出できます。 複数の変数がある場合に役立つ場合があります。 このような場合、IDE はまずこれらの変数を新しいレコードまたは bean クラスにラップしてからメソッド抽出を実行することを提案します。

![Extract method using Java records](https://resources.jetbrains.com/help/img/idea/2026.3/java_records_refactoring.animated.gif)

前:

```JAVA
class SomeClass {

    public static void main(String[] args) {
        int values[] = {1, 2, 3, 4, 5};
        int sum = 0;
        int num = 0;

        for (int n : values) {
            sum += n;
            num++;
        }

        System.out.println("Sum: " + sum + " - Num: " + num);
    }
}

```

後:

```JAVA
class SomeClass {

    public static void main(String[] args) {
        Statistics result = getStatistics();

        System.out.println("Sum: " + result.sum() + " - Num: " + result.num());
    }

    private static Statistics getStatistics() {
        int values[] = {1, 2, 3, 4, 5};
        int sum = 0;
        int num = 0;

        for (int n : values) {
            sum += n;
            num++;
        }
        Statistics result = new Statistics(sum, num);
        return result;
    }

    private record Statistics(int sum, int num) {
    }
}

```

