0

I've used following code in my project. When I evaluated it using sonar it shows Unused private method violation for the private constructor defined in ObjectTypes inner class. If I removed ObjectTypes constructor it shows Hide Utility Class Constructor violation. Please help me to find the best possible way to overcome this issue.

public final class Constants
{
  private Constants()
  {
  }

  public static final String KEY_SEPARATOR = " ~ ";      
  public static final String COMMON_SEPARATOR = " : ";

  public final class ObjectTypes
  {
    private ObjectTypes()
    {
    }
    public static final String ACTION_CODES = "Action Codes";
    public static final String ALL_ACTION_CODES = "All Action Codes";
    //more lines
  }
}
Madhujith
  • 157
  • 4
  • 18
  • I can overcome this problem by changing private constructors into protected constructors in Inner classes. However, I don't think it is a good idea to use a protected constructor in a final class. – Madhujith Sep 18 '15 at 08:06

1 Answers1

0

This is the problem:

 private Constants()
  {
  }

And this:

private ObjectTypes()
    {
    }

You do not have to declare EMPTY constructors.

David Herrero
  • 704
  • 5
  • 17
  • But, if I removed empty constructors, I can get different sonar violation saying `Hide Utility Class Constructor` for the corresponding classes – Madhujith Sep 18 '15 at 07:31
  • Check this out : http://stackoverflow.com/questions/14398747/hide-utility-class-constructor-utility-classes-should-not-have-a-public-or-def – David Herrero Sep 18 '15 at 07:33
  • All problems comes for the inner class. Is there any way to overcome `Hide Utility Class Constructor` violation for the inner class without using a private constructor? – Madhujith Sep 18 '15 at 07:42
  • I have run out of ideas. – David Herrero Sep 18 '15 at 07:57
  • If you get `Hide Utility Class Constructor` for `ObjectTypes` it means that you have only `static` fields in `ObjectTypes`, what's the point of having such class? – Massimo - SonarSource Team Sep 18 '15 at 08:06
  • 1
    @Massimo-SonarSourceTeam We use Constants class to store string literals and we used inner classes to cluster the constants class because it has huge set of literals. – Madhujith Sep 18 '15 at 08:30