Introducing a Variable, Constant, Field, or Method

See Also

When you introduce a variable, constant, field, or method in the IDE, you change a selected code fragment into a variable, constant, field, or method. Typically you do this when you want to separate a piece of code into smaller, more meaningful fragments. Creating smaller fragments can increase the reusability of your code as you can separate the parts of your code that may need to be updated more often. By giving your new method a meaningful name, you can increase the comprehensibility of your code.

For example, when you introduce a method in the IDE, you replace statements in a class with a call to a method. Before statements are replaced, the IDE opens the Introduce Method dialog box where you specify the parameters and modifiers for the method. The IDE searches your open projects for occurrences of the statements you specified and replaces the occurrences with the method call.

To introduce a variable, constant, field, or method:

  1. In the Source Editor, select the the statements you want to introduce as a new variable, constant, field, or method.
  2. Press Alt-Enter.

    Choose the appropriate menu item, such as Introduce Method.

  3. Type the name for your new item in the ext field and choose the access type from the drop-down list.
  4. Select the Declare Static checkbox if you want the new item to be static.
  5. Click OK to apply the changes to the selected files.

Troubleshooting

If you encounter an error message when introducing a method, check to see that the statements you selected meet the following criteria:

See Also
Refactoring: Quick Reference
Extracting a Superclass
Extracting an Interface
Renaming a Field or Method
Undoing Refactoring Changes

Legal Notices