I just wonder if there is a way to make public items (fields, methods) private with a simple button or something
public Group wQ;
public Group wA1;
public Group wA2;
public Group wA3;
public Group wG1;
public Group wG2;
public Group wG3;
public Group wS1;
public Group wS2;
public Group wB1;
public Group wB2;
For example make all these fields private in one step, without replacing public with private. I use IntelliJ IDEA btw.
Select a code fragment you want to extract to a method. Press Ctrl+Alt+M or from the main menu, select Refactor | Extract | Method. In the dialog that opens, configure a method options, such as visibility, parameters, and so on. You can also change a name of the method if you need.
The function you are looking for is called Encapsulate fields.
Note that this method generates getters (and, if you choose so, setters too) for all fields you select. If you don't want these getters, you can choose one of the other options or simply delete the generated getters. It seems like there is no way to opt out of creating getters.
Alternatively, you can use multi-line selections.
public
keyword.public
block.private
keyword.
As a third method, you may use Find and replace. It has some nifty options which give you great control over what you are doing.
Select Replace.
Select the text block with the public
fields.
^(\s*)public(.*)$
.$1private$2
.All replacements will now be highlighted. Review them whether they are correct. You can step through them with the blue arrows next to the upper text box.
Select Replace all.
In addition to @RAnders00's answer, there's another way that lets IntelliJ do it automatically without generating setters/getters whilst also checking if you would break code:
Rightclick on the class or package → Analyze → Run Inspection by Name → Declaration access can be weaker
IntelliJ will search your class for members that can have a reduced visiblity based on usage. After scanning you can have it updated automatically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With