All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.java.rules.squid.S2440.html Maven / Gradle / Ivy

The newest version!

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);

    //...
  }
}

See Also

  • {rule:squid:S1118} - Utility classes should not have public constructors




© 2015 - 2025 Weber Informatics LLC | Privacy Policy