Extract/Introduce variable
Refactor | Extract/Introduce | Variable
CtrlAlt0V
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 CtrlAlt0V 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.
If your Java version supports the pattern matching (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.
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.
Press CtrlAlt0V or go to Refactor | Extract/Introduce | Variable in the main menu.
Alternatively, on the toolbar that appears, click Extract and select Variable.
IntelliJ IDEA also displays next to a variable that you can use to configure more options.
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.
Select a name suggested in the popup or type your own and press Enter.
You can also declare the variable you are extracting as
final
.If you want to reassign the existing variable to a new one, press CtrlAlt0V. If you have more than one existing variable, IntelliJ IDEA displays a list to choose from.
You can press ShiftTab to change a type of the variable.
By default, this extract refactoring will be applied in the editor. To change your settings to apply the refactoring via a modal, open the Settings dialog (CtrlAlt0S) , go to Editor | Code Editing, and in the Refactorings area select In modal dialogs.
Let's extract the anotherClass.inValue()
variable that occurs twice throughout the code and give it a name number
.
Before | After |
---|---|
|
|
Before | After |
---|---|
|
|
This refactoring creates a functional expression for Java 1.8 and later versions, and an anonymous class for older versions of Java.
Select the code fragment, in this example, an argument of the
println
method.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 CtrlAltShift0T, and select Functional Variable.
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.
However, if for example, your selected code fragment depends on any local variable or a parameter
the corresponding entries would appear in the list.
When you deselect one of the parameters in the dialog, the corresponding local values will be used instead.
Configure your options and click OK.
Choose an applicable functional interface from the popup.
If you want, change a name of the extracted variable if you don't want to use the name suggested in the list.
IntelliJ IDEA creates lambda that you can use further.
tip
The functional parameter refactoring is also available.
Before | After |
---|---|
|
|
Thanks for your feedback!