Extract constants from strings and numbers with Eclipse refactorings

For readability’s sake, it’s almost always a good idea to replace magic numbers and string literals with constants. That’s all good, but it can take a bit of time to refactor these to constants, especially strings or parts of strings.

For example, in the code below we want to refactor “shovel and spade” to a private static final String called TOOLS. To do that manually would take some time. It goes even slower if we only want to extract “spade” to a constant because we first have to convert the string to a concatenation.

String tools = "shovel and spade";
...
String otherTools = "shovel and spade";

Luckily, Eclipse has a couple of ways to instantly convert literals to constants. Coupled with tools to speed up string selection and to pick out part of a string, you have the ability to create a constant in about 2 seconds flat. I’ll discuss all these features below.

Extract a constant from a string/number

There are 2 ways to extract a constant, the one uses a quick fix and the other a refactoring. I’ll show the quick fix method first and then the refactoring and discuss the (small) differences between the two. The example uses a string, but everything is true for numbers as well.

Follow these steps to use the quick fix:

  1. First select the string. The fastest way is to place the cursor on the string and press Alt+Shift+Up (Select Enclosing Element; a nifty shortcut that I discuss in Select strings and methods with a single keystroke).
  2. After selecting the string, press Ctr+1 (Quick Fix) and then select Extract to constant. Eclipse will do the following: (a) Create a private final static variable of type String with a default name, (b) replace all occurrences of that string with the constant and (c) place the cursor on the constant’s declaration to give you a chance to change the name, type and visibility of the variable using placeholders that you can Tab through.
  3. Once you’re happy with the constant details, press Enter to go back to the line on which you initiated the quick fix.

Here’s a short video with an example of using quick fix. We’ll extract a constant (called TOOLS) from a string literal (“shovel and spade”) that’s used in two places.

Note: You can use Tab to move from one placeholder to another and pressing Enter will get you back to your original line.

The other way to extract a constant is by using the Extract Constant refactoring. Again, select the string, then select Refactor > Extract Constant… (Alt+T, A) from the application menu. A dialog appears prompting you for the constant’s name, its visibility and whether to replace all occurrences of the string with the constant. After you’ve entered the details, press Enter and you’ll have your constant defined.

Here’s a short video with an example using refactoring. We’ll use the same example as above.

The differences between the two? Not much, the biggest difference being when you enter the details of the constant (ie. before the change is made or after). The refactoring dialog also provides an option to add the qualifying type name before the constant’s usage, but most of time this is redundant. I’d recommend using the quick fix, unless you’re more comfortable with dialogs.

BTW, you can assign custom keyboard shortcuts to either command by mapping either Quick Assist – Extract Constant or the command Extract Constant.

Pick out part of a string

Sometimes you’ll want to break up a string into multiple parts and convert one of those parts into a constant. Eclipse can do this automatically.

Select the part of the string you want to pick out (don’t worry about quotes), press Ctrl+1 and choose Pick out selected part of String. Eclipse will convert that part into a string with quotes, concatenate it to the rest of the string and select it. You can then use any of the Extract Constant tools above.

Here’s an example of how to use this feature. Notice how the string’s already selected so we can use the Extract Constant quick fix immediately.

Related Tips

Support Eclipse On E

3 thoughts on “Extract constants from strings and numbers with Eclipse refactorings

  1. This is great, though I do have a related question:
    What if I have a constants class where i put all of my static constants. Is there a way to have Eclipse extract the constant from the string and put it in my constants class, instead of in the current class?

    It’d be even better if Eclipse could dump the constant in the constants class, generate the getter for it, and dump the getter in the current class in place of the String. This is the manual process I’ve been doing repeatedly.
    Thanks!

    • Thanks for the compliment, Bassam.

      You can try the following:
      1. First, create your constants class if it doesn’t already exist.
      2. In the relevant class, create the constant and make sure it’s public.
      3. Position the cursor on the constant (either declaration or reference) and choose Refactor > Move…. Choose the constants class you want to move the constant to and make sure Keep original method… is deselected.
      4. In the constants class, position the cursor on the constant (either declaration or reference) and choose Refactor > Encapsulate Field….
      5. On the dialog, make sure use setter and getter is selected. Once you click Ok, Eclipse will make the constant private, create a getter for the constant and replace all references to the constant with the getter.

      That’s about as close as I could get from what you described. I’m not sure if there’s a better way, but it could still be faster.

      I hope it helps. Let me know how it works for you.

  2. Pingback: More shortcuts/quickfixes for eclipse « Bkiew's Blog