Search code examples
javaintellij-idearefactoringgetter-setter

Is there any option to automatically fix the data types in Intellij IDEA?


public class BankAccount {
    private int balance;

    public void setBalance(int x) {
        balance = x;
    }

    public int getBalance() {
        return balance;
    }
}

Here the field balance is defined as an integer and the methods which are involved with that field are also implemented according to that type. For some reason, if I change private int balance to private double balance, then I will have to change the return types and parameter types of the methods involved with that field. I can do this manually but for larger codes, that will become tedious. Is there any way to get Intellij to do that for me?


Solution

  • Place the caret on the type (in your case int) Right Click -> Refactor -> Type Migration and type the type you want to migrate and click refactor.

    Shortcuts:

    • Ctrl+Shift+F6 in Windows/*nix
    • ⌘ Command+Shift+F6 in macOS

    enter image description here