IntelliJ IDEA Refactoring: Rename, Extract, Move and More
IntelliJ IDEA can refactor code by changing its structure while updating references the IDE can identify, rather than relying on broad text replacement. Its core tools include Rename, Extract, Move, and Change Signature; use them from a declaration, selection, or call site, then review the preview and run tests.
What refactoring means in IntelliJ IDEA
A refactoring changes code organization without intending to change what the program does. Renaming a method, extracting repeated logic into a helper, moving a class to another package, or adding a method parameter are common examples. IntelliJ IDEA uses language-aware project information to locate many usages and adjust them along with the declaration.
This is different from a plain find-and-replace. A text replacement may alter comments, unrelated identifiers, or strings; a refactoring aims to distinguish symbols and their references. Its accuracy depends on the language, project setup, and whether the relevant code is indexed and available to the IDE. Reflection, generated code, external consumers, and dynamically constructed names may not be fully understood.
How to run a refactoring
Place the caret on a symbol or select the code to change. Open the Refactor menu to see applicable actions and their shortcuts. In the default keymap, common shortcuts include Shift+F6 for Rename and Ctrl+Alt+Shift+T to open the refactoring menu on Windows or Linux; macOS uses different key combinations, and keymaps can be customized. The menu is the dependable way to check the shortcut for your setup.
- Choose an action that matches the intended structural change.
- Enter the new name, destination, parameters, or other requested values.
- Inspect the affected usages or the preview when IntelliJ IDEA offers one. Check files outside the immediate package or module as well.
- Apply the change, review the resulting diff, and build or run relevant tests.
Some straightforward changes apply immediately, while others show a dialog or preview and may report conflicts. A conflict warning is useful: it can indicate that the change would create a duplicate name, alter visibility, or otherwise produce code the IDE cannot safely transform as requested. Resolve the cause instead of treating the warning as an obstacle to click through.
Rename symbols and references
Use Rename for a class, method, field, variable, parameter, or other supported symbol. IntelliJ IDEA can update references it resolves to that symbol across the project. For example, renaming calculateTotal to subtotal can update call sites as well as the method declaration.
Renaming is also useful for improving clarity after code has evolved. A name should describe the role of a value or operation, not merely its original implementation. Before applying a project-wide change, examine the preview and consider whether external code, serialized data, configuration, or reflection depends on the old name. Those relationships may not appear as ordinary source references.
Do not confuse symbol rename with renaming arbitrary text. For a string, comment, or file name, the IDE may offer a different operation or only a text-editing action. Check what will change before applying it, especially when a name appears in a public API or in data consumed by another application.
Extract methods, variables, and other code
Extraction turns a selected expression or block into a separately named element. Depending on the language and selection, IntelliJ IDEA may offer actions such as Extract Method, Extract Variable, or Extract Constant. The available choices vary with context.
For example, if a method validates an address, formats it, and writes it to a file, the formatting block might be extracted into formatAddress. The IDE analyzes values used by the selection and asks how they should be passed or returned. Review those choices: a helper with many parameters or unclear responsibilities may indicate that the selection is too large or that a different design would be clearer.
Extraction can reduce duplication and make a complex routine easier to read, but it does not automatically improve design. A tiny helper with a vague name can make code harder to follow. Choose a name that explains the responsibility, and verify that control flow, exceptions, and side effects still occur in the intended order.
Move declarations and files
Move can relocate supported classes, methods, or other declarations to a different file, package, or destination. When moving a class between packages, IntelliJ IDEA may update imports and references it can resolve. Moving a method can also require choosing a suitable destination class and handling dependencies on the original class.
Use the refactoring action where possible rather than dragging a file and assuming every language-level relationship will be updated. Inspect package declarations, imports, visibility, tests, resources, and build configuration after a move. A file move may also affect conventions or tools that infer behavior from paths, so run the project’s build and relevant tests.
Change Signature and related transformations
Change Signature modifies a method or constructor’s parameter list and can update recognized call sites. Depending on the language and operation, it can add, remove, reorder, or rename parameters, and may provide a default value or expression for new arguments. For example, adding a currency parameter to formatPrice(amount) requires deciding what existing calls should pass.
Signature changes deserve extra care when a method is public, overridden, implemented by multiple classes, or called by code outside the project. The IDE can update many known usages, but it cannot guarantee that separate modules, plugins, generated sources, or external clients are updated. Check inheritance relationships and implementations, not just the visible call sites.
Other useful refactorings include changing a local variable’s type where supported, introducing a parameter, pulling members up or pushing them down a class hierarchy, and converting between certain code constructs. Availability depends on language support and context; inspect the Refactor menu rather than assuming every operation applies everywhere.
Common problems and safe habits
- References are missed: confirm the project is open and indexed, and search for reflective or string-based uses that symbol analysis may not find.
- The action is unavailable: place the caret on the declaration or select a valid expression, then check the language and context.
- A preview shows unexpected files: inspect each change. Some usages may be outside the intended scope, while others may reveal dependencies worth addressing.
- The project stops compiling: review the diff, resolve conflicts, and check imports, method overrides, generated code, and build configuration.
- A change is hard to review: perform one conceptual refactoring at a time and commit or otherwise preserve a clean checkpoint before larger edits.
Refactoring is not a substitute for tests or version control. A successful IDE operation means the transformation was applied, not that behavior is unchanged in every runtime situation. Build the project, run focused tests, and review the diff; for externally visible APIs, check compatibility requirements separately.
Frequently Asked Questions
Does IntelliJ IDEA update every reference when I rename a method?
It updates usages it can resolve in the project. References formed through reflection, strings, generated sources, or external clients may need manual attention.
How do I find the refactorings available for selected code?
Put the caret on a symbol or select an expression, then open the Refactor menu. The available actions depend on the language and code context.
Can I undo a refactoring?
Use the IDE’s Undo command if the change is still in the current editing history. Version control provides an additional way to inspect and restore changes; review the diff before reverting work.
What is the difference between Rename and Change Signature?
Rename changes a symbol’s name and recognized references. Change Signature alters a method or constructor’s parameters and can update recognized calls to match.
Why does IntelliJ IDEA not offer Extract Method?
The selection may not form a valid extractable block, or the language and context may not support that operation. Adjust the selection and check the Refactor menu for alternatives.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.