Ali Nem
Ali Nem

Reputation: 5540

How to move static variables from a class to another class when refactoring in IntelliJ or Android Studio?

Suppose Class A with some static parameters:

public Class A {
    public static final String KEY_1="key1"
    public static final String KEY_2="key2"
    ...
}

The static parameters are used repeatedly in the project. When refactoring, I want to move them to another class, say Class B; but doing so I have to update every use case of them, e.g. A.KEY_1 to B.KEY_2, manually.

Is there any way to do so automatically in Android Studio or IntelliJ? What I have already tried is selecting the lines containing the parameters and pressing F6 which opens a refactoring dialog, but going through it does not get me to what I want.

Upvotes: 13

Views: 2932

Answers (1)

Mureinik
Mureinik

Reputation: 311163

IntelliJ is really good at refactoring. Place your caret on the constant you want to move and do the following:

  1. Refactor -> Move (with the default key bindings, this can also be done by pressing F6).
  2. You'll get a dialog asking you for the fully qualified name of the class you want to move it to (don't worry - just start typing and IntelliJ will autocomplete for you) and a menu to choose the visibility modified you want to assign to the variable/constant.
  3. Click the "Refactor" button
  4. Enjoy

Upvotes: 14

Related Questions