A Cool Refactoring Hidden in IntelliJ IDEA
This is about replacing field access with a getter
By default, there’s a visible refactoring option for this through alt+insert
- getter
.
The problem is that if a field is already used in many places, just creating a getter is not enough. You also need to replace direct access with this getter everywhere.
Fortunately, JetBrains has thought of this!
Press ctrl+shift+A
, enter and open the refactor this
action.
In the menu, there are many different refactorings. We select encapsulate
.
A dialog will open where you select the needed fields, tick the getters/setters, and click refactor
.
Done!
As we can see, direct access has been replaced with the getter throughout the code.