IntelliJ IDEA 2018.1 Help

Extract Variable

If you come across an expression that is hard to understand or it is duplicated in several places throughout you code, the Extract Variable refactoring can help you deal with those problems 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.

  1. In the editor, select an expression or its part that you want to extract. You can also place the cursor within the expression, in this case IntelliJ IDEA offers you a list of potential code selections.
  2. Press Ctrl+Alt+V or on the main menu, select Refactor | Extract | Extract Variable.
  3. Select a name suggested in the pop-up or type your own and press Enter.
    extract var
    If IntelliJ IDEA finds more than one occurrence, it lets you specify whether or not you want to extract this variable for all of them.

    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. If you have more than one existing variable, IntelliJ IDEA displays a list to choose from.

    You can press Shift+Tab to change a type of the variable.

    change type for var

If the Enable in-place mode option is not selected in the Settings/Preferences | Editor |General dialog (Refactorings section), IntelliJ IDEA opens the Extract Variable dialog for this refactoring.

extract var dialog

Example

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

BeforeAfter
public void method() { int a = 1; ... int b = a + anotherClass.intValue(); int c = b + anotherClass.intValue(); }
public void method() { int a = 1; ... int number = anotherClass.intValue(); int b = a + number; int c = b + number; }
BeforeAfter
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.

  1. Select the code fragment, in this example, an argument of the println method.
    argument println
  2. In the main menu, select Refactor | Extract | Functional Variable.
    IntelliJ IDEA opens the Extract Functional Variable dialog.
    extrct funtional var
  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
    However, if for example, your selected code fragment depends on any local variable or a parameter
    sample string
    the corresponding entries would appear in the list.
    local variable and parameter
    When you deselect one of the parameters in the dialog, the corresponding local values will be used instead.
    when param deselected
    Configure your options and click OK.
  4. Choose an applicable functional interface from the pop-up.
    functional interface
  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
  6. IntelliJ IDEA creates lambda that you can use further.
    composed function

Example

BeforeAfter
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()); } }
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)); } }
Last modified: 24 July 2018

See Also