rspec/rules/S2440/rule.adoc
2022-02-04 16:28:24 +00:00

56 lines
964 B
Plaintext

``++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
[source,text]
----
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
[source,text]
----
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);
//...
}
}
----
== See Also
* S1118 - Utility classes should not have public constructors