description Extract Constant Overview
This tool identifies literal values (like '3.14159' or 'MAX_RETRIES') used repeatedly throughout the code. It extracts these 'magic numbers' into a dedicated, named constant field or variable. This centralizes configuration, making global changes trivial and preventing inconsistencies across the codebase.
help Extract Constant FAQ
What does JetBrains Extract Constant change in Java code?
It moves a selected literal or expression into a named constant, such as converting repeated "string" values into a private static final field. IntelliJ IDEA can then replace matching occurrences with that field.
What is the Extract Constant shortcut in IntelliJ IDEA?
On Windows and Linux, the standard shortcut is Ctrl+Alt+C. It is also available through Refactor, Extract, Constant, while macOS uses its corresponding Command-based shortcut.
Can Extract Constant replace every matching literal at once?
Yes, JetBrains IDEs can offer to replace all suitable occurrences of the selected expression. You should review the scope because two identical literals can represent different concepts and may not belong under one constant.
Can I choose the constant's visibility or move it to another class?
The full Extract Constant dialog lets you choose options such as private or public visibility. In IntelliJ IDEA, invoking Ctrl+Alt+C twice opens the dialog and can also expose an option to place the constant in another class.
explore Explore More
Similar to Extract Constant
See all arrow_forwardReviews & Comments
Write a Review
Be the first to review
Share your thoughts with the community and help others make better decisions.