我得到了这个checkstyle错误:
'serverURL' hides a field
在这里
private static void setServerURL(final String serverURL) {
Utility.serverURL = serverURL;
}
可能的原因是什么,如何解决它?
发布于 2011-10-15 13:46:38
已经有一个变量定义的serverURL
可用于此方法(除了您正在接受的形参之外)。这就是所谓的“影子”。
我认为大多数Java程序员都忽略了这个检查,因为它实际上并不那么令人困惑。
例如,这将触发错误:
public class Foo {
private int bar = 0;
public void someMethod(int bar) {
// There are two bars! All references in this method will use the parameter bar,
// unless they are explicitly prefixed with 'this'.
this.bar = bar;
}
}
发布于 2015-04-26 22:57:39
我认为在构造函数和setter中,set字段名称与setter参数名称相同是很常见的。这就是我推荐这种配置的原因:
<module name="HiddenField" >
<property name="ignoreSetter" value="true" />
<property name="ignoreConstructorParameter" value="true" />
</module>
这样,其他隐藏字段的情况仍然是被禁止的。
发布于 2012-10-18 18:17:07
参数和静态字段具有相同的名称。只需重命名其中一个即可。有些人遵循以p
作为所有参数前缀的命名约定。然后,您将使用serverURL
作为字段名,将pServerURL
作为参数名。或者,您可以简单地关闭检查。
https://stackoverflow.com/questions/7776046
复制相似问题