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

org.sonar.l10n.squidjava.rules.squid.S1171.html Maven / Gradle / Ivy

Non-static initializers are rarely used, and can be confusing for most developers. When possible, they should be refactored into standard constructors or field initializers.

The following code:

class MyClass {
  private static final Map MY_MAP = new HashMap() {

    // Non-Compliant - HashMap should be extended only to add behavior, not for initialization
    {
      put("a", "b");
    }

  };
}

could be refactored using Guava into:

class MyClass {
  // Compliant
  private static final Map MY_MAP = ImmutableMap.of("a", "b");
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy