Why this matters
Utility classes contain only static members and should not be instantiated. Mark them as `abstract` or provide a private constructor.
Make utility classes non-instantiable by providing a private constructor or marking them as `abstract`.
Utility classes contain only static members and should not be instantiated. Mark them as `abstract` or provide a private constructor.
Side-by-side examples engineers can pattern-match during review.
class StringUtils { // Noncompliant
static String concatenate(String s1, String s2) {
return s1 + s2;
}
}class StringUtils { // Compliant
StringUtils._() {
throw Exception('Utility class');
}
static String concatenate(String s1, String s2) {
return s1 + s2;
}
}class StringUtils { // Noncompliant
static String concatenate(String s1, String s2) {
return s1 + s2;
}
}class StringUtils { // Compliant
StringUtils._() {
throw Exception('Utility class');
}
static String concatenate(String s1, String s2) {
return s1 + s2;
}
}From the same buckets as this rule.