可变字段不应该是“公共静态”

2024-01-27

我收到下面一行的 sonarQube 错误,有专家建议如何解决这个问题吗?提前致谢

    protected static final String [] COLUMN_NAMES = new String[]{"date","customerNumber","customerName",
            "account","emailAdress","mobilePhoneNumber","emailStatus"};

您可以将此数组更改为private多变的。

然后添加一个static返回此数组的副本或不可变的方法List由这个数组支持。

例如:

private static final String [] COLUMN_NAMES = new String[]{"date","customerNumber","customerName",
        "account","emailAdress","mobilePhoneNumber","emailStatus"};

protected static List<String> getColumnNames() {
    return Collections.unmodifiableList(Arrays.asList(COLUMN_NAMES));
}

或者您可以将数组变量替换为不可修改的变量List而不是使用该方法。这会更有效率(因为List将被创建一次,而不是在每次调用时创建static方法):

protected static List<String> COLUMN_NAMES = Collections.unmodifiableList(Arrays.asList("date","customerNumber","customerName",
        "account","emailAdress","mobilePhoneNumber","emailStatus"));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

可变字段不应该是“公共静态” 的相关文章

随机推荐