如何使代码符合:移动此变量以符合Java代码约定

问题描述

我有以下课程:

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用变量在所有三行中标记错误:

移动此变量以符合Java代码约定。

如何使代码与SonarQube兼容?

我正在将SonarLint用于Eclipse v2.6.0。

解决方法

通过RSPEC-1213 The members of an interface or class declaration should appear in a pre-defined order规则发现了问题。描述说:

根据Oracle定义的Java代码约定,类或接口声明的成员应按以下顺序出现在源文件中:

  • 类和实例变量
  • 构造函数
  • 方法

您的代码在变量之前包含一个构造函数。有效代码:

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() {}
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...