IntelliJ IDEA 11.1 Web Help

8.0+
Some features described here are available in Ultimate edition only.

The Introduce Variable refactoring puts the result of the selected expression into a variable. It declares a new variable and uses the expression as an initializer. The original expression is replaced with the new variable (see the examples below).

In Java, the type of the new variable corresponds to that returned by the expression. There is an option of declaring the new variable as final.

The following is available only in the Ultimate edition of IntelliJ IDEA

In JavaScript and ActionScript (if supported by your version of IntelliJ IDEA), you can replace an expression with a variable or a constant. For JavaScript 1.7 or a later version, there is also an option of introducing a local variable. Refer to the section Introduce Variable in JavaScript for details.

To introduce a new variable, you can use:

You can select the expression to be replaced with a variable yourself. You can as well use smart expression selection. In this case IntelliJ IDEA will help you select the desired expression.

Java examples

Before After
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;
}
    
public void method() {
    int a = anotherClass.innerClass.i;
    int b = anotherClass.innerClass.j;
}
public void method() {
    AnotherClass.InnerClass aClass = anotherClass.innerClass;
    int a = aClass.i;
    int b = aClass.j;
}
    
static void printNames(final String fullName) {
    System.out.println(fullName.substring(fullName.indexOf(" ") + 1));
    System.out.println(fullName.substring(0, fullName.indexOf(" ")));
}
                    
static void printNames(final String fullName) {
    int firstNameEndIndex = fullName.indexOf(" ");
    System.out.println(fullName.substring(firstNameEndIndex + 1));
    System.out.println(fullName.substring(0, firstNameEndIndex));
}
                    
To introduce a variable using in-place refactoring
  1. In the editor, select the expression to be replaced with a variable. You can do that yourself or use the smart expression selection feature to let IntelliJ IDEA help you. So, do one of the following:
    • Highlight the expression. Then choose Refactor | Introduce Variable from the main or the context menu. Alternatively, press Ctrl+Alt+VCommand Alt V.

      introduceVariableJavaSelectExpression

    • Place the cursor before or within the expression. Choose Refactor | Introduce Variable from the main or the context menu, or press Ctrl+Alt+VCommand Alt V.

      introduceVariableJavaSmartSelectExpressionStart

      In the Expressions pop-up menu, select the expression. To do that, click the required expression. Alternatively, use the UpUp and DownDown arrow keys to navigate to the expression of interest, and then press EnterEnter to select it.

      introduceVariableJavaSmartSelectExpression

      Note

      The Expressions pop-up menu contains all the expressions appropriate for the current cursor position in the editor.

      When you navigate through the suggested expressions in the pop-up, the code highlighting in the editor changes accordingly.

  2. If more than one occurrence of the selected expression is found, select Replace this occurrence only or Replace all occurrences in the Multiple occurrences found pop-up menu. To select the required option, just click it. Alternatively, use the UpUp and DownDown arrow keys to navigate to the option of interest, and press EnterEnter to select it.

    introduceVariableJavaOccurrences

  3. Specify the name of the variable. Do one of the following:
    • Select one of the suggested names from the pop-up list. To do that, double-click the suitable name. Alternatively, use the UpUp and DownDown arrow keys to navigate to the name of interest, and EnterEnter to select it.

      introduceVariableJavaSelectName

    • Edit the name by typing. The name is shown in the box with red borders and changes as you type. When finished, press EscapeEscape.

      introduceVariableJavaTypeName

To introduce a variable using the Introduce Variable dialog

If the Enable in place refactorings check box is cleared on the Editor settings, the Introduce Variable refactoring is performed by means of the Introduce Variable dialog box.

enableInplaceRefactoringCleared

  1. In the editor, select the expression to be replaced with a variable. You can do that yourself or use the smart expression selection feature to let IntelliJ IDEA help you. So, do one of the following:
    • Highlight the expression. Then choose Refactor | Introduce Variable from the main or the context menu. Alternatively, press Ctrl+Alt+VCommand Alt V.
    • Place the cursor before or within the expression. Choose Refactor | Introduce Variable from the main or the context menu, or press Ctrl+Alt+VCommand Alt V.

      In the Expressions pop-up menu, select the expression. To do that, click the required expression. Alternatively, use the UpUp and DownDown arrow keys to navigate to the expression of interest, and then press EnterEnter to select it.

      Note

      The Expressions pop-up menu contains all the expressions appropriate for the current cursor position in the editor.

      When you navigate through the suggested expressions in the pop-up, the code highlighting in the editor changes accordingly.

  2. In the Introduce Variable dialog:
    1. Specify the variable name next to Name. You can select one of the suggested names from the list or type the name in the Name box.
    2. If more than one occurrence of the selected expression is found, you can select to replace all the found occurrences by selecting the corresponding check box. If you want to replace only the current occurrence, clear the Replace all occurrences check box.
    3. If you want to declare the new variable final, select the Declare final check box. (This option is available only for Java.)
    4. For ActionScript, you can choose to introduce a constant rather than a variable. To do that, select the Make constant check box. (Note that ActionScript is not supported in IntelliJ IDEA Community Edition.)
    5. Click OK.

Note

For the JavaScript procedure, refer to the section Introduce Variable in JavaScript.

See Also

Procedures:

Reference:

Web Resources: