# Extract/Introduce variable

> **TL;DR**
> `Refactor | Extract/Introduce | Variable`
>
>
>
> `Ctrl+Alt+V` (Windows), `⌘ ⌥ V` (macOS), `⌘ ⌥ V` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ V` (macOS System Shortcuts), `Ctrl+Alt+V` (XWin), `Ctrl+Alt+V` (GNOME), `Alt+Shift+V` (KDE), `Ctrl+Alt+V` (Emacs), `Ctrl+Alt+V` (Sublime Text), `Ctrl+Alt+V` (Sublime Text (macOS)), `Alt+Shift+V` (NetBeans), `Ctrl+R, V` (Visual Studio), `⌘ R, V` (Visual Studio (macOS)), `Alt+Shift+L` (Eclipse), `⌘ ⌥ L` (Eclipse (macOS))

If you come across an expression that is hard to understand, or it is duplicated in several places throughout your code, the Extract Variable refactoring `Ctrl+Alt+V` (Windows), `⌘ ⌥ V` (macOS), `⌘ ⌥ V` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ V` (macOS System Shortcuts), `Ctrl+Alt+V` (XWin), `Ctrl+Alt+V` (GNOME), `Alt+Shift+V` (KDE), `Ctrl+Alt+V` (Emacs), `Ctrl+Alt+V` (Sublime Text), `Ctrl+Alt+V` (Sublime Text (macOS)), `Alt+Shift+V` (NetBeans), `Ctrl+R, V` (Visual Studio), `⌘ R, V` (Visual Studio (macOS)), `Alt+Shift+L` (Eclipse), `⌘ ⌥ L` (Eclipse (macOS)) can help you deal with those problems by placing the result of such expression or its part into a separate variable that is less complex and easier to understand. Plus, it reduces the code duplication.

Starting with Java 1.8 and later versions, IntelliJ IDEA also lets you [extract a functional type variable](#extract_functional_variable).

If your Java version supports the [pattern matching](http://cr.openjdk.java.net/~gbierman/jep305/jep305-20191021/specs/patterns-instanceof-jls.html) (Java 14 preview or higher), and you are extracting a cast expression under the `instanceof` check, IntelliJ IDEA may introduce a pattern variable at that `instanceof` check instead of a normal local variable. Also, you can inline pattern variable since the Java 14 version. In this case, all the occurrences will be replaced with old-style cast expression.

You can use the Introduce Variable refactoring to extract variadic arguments into a new slice variable.

Procedure:

1. In the editor, select an expression or its part that you want to extract . You can also place the caret within the expression, in this case IntelliJ IDEA offers you a list of potential code selections.

2. Press `Ctrl+Alt+V` (Windows), `⌘ ⌥ V` (macOS), `⌘ ⌥ V` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ V` (macOS System Shortcuts), `Ctrl+Alt+V` (XWin), `Ctrl+Alt+V` (GNOME), `Alt+Shift+V` (KDE), `Ctrl+Alt+V` (Emacs), `Ctrl+Alt+V` (Sublime Text), `Ctrl+Alt+V` (Sublime Text (macOS)), `Alt+Shift+V` (NetBeans), `Ctrl+R, V` (Visual Studio), `⌘ R, V` (Visual Studio (macOS)), `Alt+Shift+L` (Eclipse), `⌘ ⌥ L` (Eclipse (macOS)) or go to `Refactor | Extract/Introduce | Variable` in the main menu.

Alternatively, on the [toolbar](working-with-source-code.html#floating_toolbar) that appears, click Extract and select Variable.

IntelliJ IDEA also displays ![the Gear icon](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.expui.general.settings.svg) next to a variable that you can use to configure more options.

![More options](https://resources.jetbrains.com.cn/help/img/idea/2026.2/introduce_var_more_options.png)

If IntelliJ IDEA finds more than one occurrence, it lets you specify a scope and extract just a part of the found occurrences and not just all of them.

![Introduce variable occurrences scope](https://resources.jetbrains.com.cn/help/img/idea/2026.2/introduce_variable_occurrences_scope.png)

3. Select a name suggested in the popup or type your own and press `Enter` (Windows), `⏎` (macOS), `⏎` (IntelliJ IDEA Classic (macOS)), `⏎` (macOS System Shortcuts), `Enter` (XWin), `Enter` (GNOME), `Enter` (KDE), `Enter` (Emacs), `Enter` (Sublime Text), `⏎` (Sublime Text (macOS)), `Enter` (NetBeans), `Enter` (Visual Studio), `⏎` (Visual Studio (macOS)), `Enter` (Eclipse), `⏎` (Eclipse (macOS)).

![Extracting a variable ](https://resources.jetbrains.com.cn/help/img/idea/2026.2/extract_var.png)

You can also declare the variable you are extracting as `final`.

If you want to reassign the existing variable to a new one, press `Ctrl+Alt+V` (Windows), `⌘ ⌥ V` (macOS), `⌘ ⌥ V` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ V` (macOS System Shortcuts), `Ctrl+Alt+V` (XWin), `Ctrl+Alt+V` (GNOME), `Alt+Shift+V` (KDE), `Ctrl+Alt+V` (Emacs), `Ctrl+Alt+V` (Sublime Text), `Ctrl+Alt+V` (Sublime Text (macOS)), `Alt+Shift+V` (NetBeans), `Ctrl+R, V` (Visual Studio), `⌘ R, V` (Visual Studio (macOS)), `Alt+Shift+L` (Eclipse), `⌘ ⌥ L` (Eclipse (macOS)). If you have more than one existing variable, IntelliJ IDEA displays a list to choose from.

You can press `Shift+Tab` (Windows), `⇧ ⇥` (macOS), `⇧ ⇥` (IntelliJ IDEA Classic (macOS)), `⇧ ⇥` (macOS System Shortcuts), `Shift+Tab` (XWin), `Shift+Tab` (GNOME), `Shift+Tab` (KDE), `Shift+Tab` (Emacs), `Shift+Tab` (Sublime Text), `⇧ ⇥` (Sublime Text (macOS)), `Shift+Tab` (NetBeans), `Shift+Tab` (Visual Studio), `⇧ ⇥` (Visual Studio (macOS)), `Shift+Tab` (Eclipse), `⇧ ⇥` (Eclipse (macOS)) to change a type of the variable.

![Declaring a variable final](https://resources.jetbrains.com.cn/help/img/idea/2026.2/change_type_for_var.png)

By default, this extract refactoring will be applied in the editor. To change your settings to apply the refactoring [via a modal](extract-variable-refactoring-dialog.html),   open the Settings dialog (`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` (NetBeans), `Ctrl+Alt+S` (Visual Studio), `⌘ Comma` (Visual Studio (macOS)), `Ctrl+Alt+S` (Eclipse), `⌘ Comma` (Eclipse (macOS))), go to Editor | Code Editing, and in the Refactorings area select In modal dialogs.

## Example

Let's extract the `anotherClass.inValue()` variable that occurs twice throughout the code and give it a name `number`.

Java:

| Before | After |
| --- | --- |
|     ```JAVA public void method() {     int a = 1; ...     int b = a + anotherClass.intValue();     int c = b + anotherClass.intValue(); } ```    |     ```JAVA public void method() {     int a = 1; ...     int number = anotherClass.intValue();     int b = a + number;     int c = b + number; } ```    |

Kotlin:

| Before | After |
| --- | --- |
|     ```ACTIONSCRIPT func method() {      val a = 1      val b = a + anotherClass!!.intValue()      val c = b + anotherClass!!.intValue() } ```    |     ``` func method() {      val a = 1      val number = anotherClass!!.intValue()      val b = a + number      val c = b + number } ```    |

## Extract functional variable

This refactoring creates a functional expression for Java 1.8 and later versions, and an anonymous class for older versions of Java.

Procedure:

1. Select the code fragment, in this example, an argument of the `println` method.

![argument_println.png](https://resources.jetbrains.com.cn/help/img/idea/2026.2/argument_println.png)

2. In the main menu, go to `Refactor | Extract/Introduce | Functional Variable`.

IntelliJ IDEA opens the Extract Functional Variable dialog.

Alternatively, from the context menu in the editor, select `Refactor | Refactor This` `Ctrl+Alt+Shift+T` (Windows), `⌃ T` (macOS), `⌘ ⌥ ⇧ T` (IntelliJ IDEA Classic (macOS)), `⌃ S` (macOS System Shortcuts), `Ctrl+Alt+Shift+T` (XWin), `Ctrl+Alt+Shift+T` (GNOME), `Ctrl+Alt+Shift+T` (KDE), `Ctrl+Alt+Shift+T` (Emacs), `Ctrl+Alt+Shift+T` (Sublime Text), `⌃ T` (Sublime Text (macOS)), `Ctrl+Alt+Shift+T` (NetBeans), `Ctrl+Shift+R` (Visual Studio), `⌘ ⇧ R` (Visual Studio (macOS)), `Ctrl+Alt+Shift+T` (Eclipse), `⌃ T` (Eclipse (macOS)), and select Functional Variable.

![extrct_funtional_var.png](https://resources.jetbrains.com.cn/help/img/idea/2026.2/extrct_funtional_var.png)

3. When the selected code depends on instance fields, like in the example, the Pass field as params checkbox will appear, and you can pass a parameter in a place of fields.

![extrct_funtional_var_parameter.png](https://resources.jetbrains.com.cn/help/img/idea/2026.2/extrct_funtional_var_parameter.png)

However, if for example, your selected code fragment depends on any local variable or a parameter

![sample_string.png](https://resources.jetbrains.com.cn/help/img/idea/2026.2/sample_string.png)

the corresponding entries would appear in the list.

![local_variable_and_parameter.png](https://resources.jetbrains.com.cn/help/img/idea/2026.2/local_variable_and_parameter.png)

When you deselect one of the parameters in the dialog, the corresponding local values will be used instead.

![when_param_deselected.png](https://resources.jetbrains.com.cn/help/img/idea/2026.2/when_param_deselected.png)

Configure your options and click OK.

4. Choose an applicable functional interface from the popup.

![functional_interface.png](https://resources.jetbrains.com.cn/help/img/idea/2026.2/functional_interface.png)

5. If you want, change a name of the extracted variable if you don't want to use the name suggested in the list.

![changing_variable_name.png](https://resources.jetbrains.com.cn/help/img/idea/2026.2/changing_variable_name.png)

6. IntelliJ IDEA creates lambda that you can use further.

![composed_function.png](https://resources.jetbrains.com.cn/help/img/idea/2026.2/composed_function.png)

> **Tip:**
> The [functional parameter](extract-parameter.html#functional_parameter) refactoring is also available.

### Example

| Before | After |
| --- | --- |
|     ```JAVA import java.util.List;  public class PrintAction implements Runnable {     private List<String> data;      public PrintAction(List<String> data) {         this.data = data;     }      public void run() {         System.out.println("Data: " + data.toString());     } } ```    |     ```JAVA import java.util.List; import java.util.function.Function;  public class PrintAction implements Runnable {     private List<String> data;      public PrintAction(List<String> data) {         this.data = data;     }      public void run() {         Function<List<String>, String> presenter = (p) -> "Data: " + p.toString();         System.out.println(presenter.apply(data));     } } ```    |

## See also

### Reference

[Extract Variable dialog](extract-variable-refactoring-dialog.html)

### Procedures

[Selecting text in the editor](working-with-source-code.html) [Rename refactorings](rename-refactorings.html)  [Extract Variable in JavaScript](specific-javascript-refactorings.html#javascript_extract_variable)

