Instantiation of utility class
Reports instantiation of utility classes using the new
keyword.
In utility classes, all fields and methods are static
. Instantiation of such classes is most likely unnecessary and indicates a mistake.
Example:
class MyUtils {
public static double cube(double x) {
return x * x * x;
}
}
class Main {
public static void main(String[] args) {
// Instantiation of utility class
MyUtils utils = new MyUtils();
}
}
To prevent utility classes from being instantiated, it's recommended to use a private
constructor.
Locating this inspection
- By ID
Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.
InstantiationOfUtilityClass- Via Settings dialog
Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.
Inspection Details | |
---|---|
By default bundled with: | |
Can be installed with plugin: | Java, 242.22892 |
Last modified: 11 September 2024