static
methods can be accessed without an instance of the enclosing class, so there’s no reason to instantiate a class that has only
static
methods.
Noncompliant code example
public class TextUtils {
public static String stripHtml(String source) {
return source.replaceAll("<[^>]+>", "");
}
}
public class TextManipulator {
// ...
public void cleanText(String source) {
TextUtils textUtils = new TextUtils(); // Noncompliant
String stripped = textUtils.stripHtml(source);
//...
}
}
Compliant solution
public class TextUtils {
public static String stripHtml(String source) {
return source.replaceAll("<[^>]+>", "");
}
}
public class TextManipulator {
// ...
public void cleanText(String source) {
String stripped = TextUtils.stripHtml(source);
//...
}
}