IntelliJ IDEA
 
Get IntelliJ IDEA

Extract/Introduce variable

Last modified: 23 October 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.

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.