Extract field
Refactor | Extract/Introduce | Field
CtrlAlt0F
The Extract Field refactoring lets you declare a new field and initialize it with the selected expression. The original expression is replaced with the usage of the field.
Place the caret within a piece of code you want to extract into a field.
Press CtrlAlt0F or go to Refactor | Extract/Introduce | Field in the main menu.
Select an expression you want to introduce as a field.
If IntelliJ IDEA detects more than one occurrence in your code, it lets you specify which occurrences to replace.
You can press CtrlAlt0F twice to open the Extract Field dialog where you can specify additional details, such as visibility options, or options for initializing your variable.
Let's extract the anotherClass.intValue();
variable into a field. As a result, IntelliJ IDEA changes the selected variable name to number
and declares it as the private int number
field.
Before | After |
---|---|
|
|
Thanks for your feedback!