Search code examples
sonarqubesonarlint

How to make the code compliant with: Move this variable to comply with Java Code Conventions


I have the following class:

public final class AppConst {
    private AppConst() {}
        
    public static final String READ_ERROR = "READ";
    public static final String PROCESS_ERROR = "PROCESS";
    public static final String WRITE_ERROR = "WRITE";
}

SonarQube marks errors on all three lines with the variables:

Move this variable to comply with Java Code Conventions.

How to make the code compliant with SonarQube?

I'm using SonarLint for Eclipse v2.6.0.


Solution

  • The issue is found by the RSPEC-1213 The members of an interface or class declaration should appear in a pre-defined order rule. The description says:

    According to the Java Code Conventions as defined by Oracle, the members of a class or interface declaration should appear in the following order in the source files:

    • Class and instance variables
    • Constructors
    • Methods

    Your code contains a constructor before the variables. Valid code:

    public final class AppConst {   
        public static final String READ_ERROR = "READ";
        public static final String PROCESS_ERROR = "PROCESS";
        public static final String WRITE_ERROR = "WRITE";
    
        private AppConst() {}
    }