gpt4 book ai didi

java - 可变字段不应为 "public static"

转载 作者:搜寻专家 更新时间:2023-10-31 19:51:48 26 4
gpt4 key购买 nike

我收到下面一行的 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"));

关于java - 可变字段不应为 "public static",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53764748/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com