Q:

Avoid Duplicate Literals Sonar Error

I'm using SonarQube to verify and inspect my Java code, and i've encountered an issue with  Avoid Duplicate Literals in an Enum class type, here is an exemple :
public enum Products {

  A ("Product A"),
  C ("Product A"),
  D ("Product B"),
  P ("Product B");

  private String name = "";

  Products (String name){
    this.name = name;
  }

  public String toString(){
    return name;
  }
}

Sonar is telling me to declare the Strings 'Product A' and 'Product B' as a constant field, but you can't declare variables in Enum type class.

A:

You can declare the constant outside of the enum:
private static final String TYPE_A_NAME = "Type A";

public enum Type {

    TYPEA(TYPE_A_NAME), TYPEB("B");

    private String value;

    Type(String value) {

    }
}

