Example
| Before | After |
|---|---|
public void method() { String str ="str"; String aString = returnString().concat(str); System.out.println(aString); } |
public void method() { String str ="str"; System.out.println(aString(str)); } private String aString(String str) { return returnString().concat(str); } |
- In the editor, position the caret at the name of the local variable you want to be refactored.
- On the main menu, or on the context menu of the selection, choose .
Replace Temp with Query
dialog box appears.
Note
Selecting a local variable, make sure that its value is not modified later in the code. Otherwise the error message appears.
- Specify the name for the extracted method.
- To declare the method static, select the Declare static check box. This option is enabled when the initial expression is static.
- In the Parameters section, select the parameters to be used in the extracted method. The parameters are all checked by default. If unchecked, the appropriate value will be used as a local variable in the extracted method.
- Check the result in the Signature Preview pane and click OK to create the method.

