IntelliJ IDEA
 
Get IntelliJ IDEA
You are viewing the documentation for an earlier version of IntelliJ IDEA.

Extract/Introduce variable

Last modified: 10 August 2022

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 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.

If the In modal dialogs refactoring option is selected in the Refactorings area on the Code Editing page of the Settings/Preferences dialog (Ctrl+Alt+S), IntelliJ IDEA opens the Extract Variable dialog for this refactoring.

Extract var dialog