Java
Instantiation of utility class
Warning
New
Last modified: 03 December 2024 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.
- 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
Inspection Details | |
---|---|
By default bundled with: | |
Can be installed with plugin: | Java, 243.23126 |
Thanks for your feedback!
Was this page helpful?