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

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

The purpose of the Java Collections API is to provide a well defined hierarchy of interfaces in order to hide all implementation details. Implementating classes must be used to instantiate a new collection, but the result of an instantiation should immediately be stored in a variable whose type is a Java Collection interface.

Noncompliant Code Example

public class Employees {
  private HashSet<Employee> employees = new HashSet<Employee>();  // Noncompliant - "employees" should have type "Set" rather than "HashSet"

  public HashSet<Employee> getEmployees() {                       // Noncompliant
    return employees;
  }
}

Compliant Solution

public class Employees {
  private Set<Employee> employees = new HashSet<Employee>();      // Compliant

  public Set<Employee> getEmployees() {                           // Compliant
    return employees;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy