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

Extract/Introduce variable

Last modified: 11 February 2024

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.

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

Extract Variable dialog