Rom*_*omi 55 java eclipse checkstyle
我收到这个checkstyle错误:
'serverURL' hides a field
Run Code Online (Sandbox Code Playgroud)
在这
private static void setServerURL(final String serverURL) {
Utility.serverURL = serverURL;
}
Run Code Online (Sandbox Code Playgroud)
可能是什么原因,以及如何解决它?
Cor*_*all 48
已经定义了一个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;
}
}
Run Code Online (Sandbox Code Playgroud)
Won*_*abo 35
我认为在构造函数和setter中,set字段名称与setter参数名称相同是很常见的.这就是我推荐这种配置的原因:
<module name="HiddenField" >
<property name="ignoreSetter" value="true" />
<property name="ignoreConstructorParameter" value="true" />
</module>
Run Code Online (Sandbox Code Playgroud)
这样,其他隐藏的野外案件仍然是被禁止的.
参数和静态字段具有相同的名称.只需重命名其中一个.有些人遵循为所有参数添加前缀的命名约定p
.然后你将有serverURL
字段名称和pServerURL
参数名称.或者你可以简单地关掉支票.
小智 7
我通过在eclipse中禁用它来解决它.当我登陆这个页面时,我一直在寻找如何做到这一点.我没有在前10个谷歌查询中找到答案所以我不得不弄清楚它的方法.对于那些正在寻找它的人来说,我就是这样做的:
打开
Eclipse的>首选项>的Checkstyle
找到您正在使用的checkstyle配置(您可能已设置或正在使用默认设置,在这种情况下,最好创建自己的副本然后进行编辑).选择该选项,然后单击右侧的配置按钮.在列表中找到以下配置:
编码问题>隐藏字段
打开配置(UI中有一个名为"open"的按钮).
取消选择"参数声明".单击OK然后单击OK,然后单击OK.
小智 5
只需使用您的方法更改您的参数名称
private static void setServerURL(final String serverURL) {
Utility.serverURL = serverURL;
}
Run Code Online (Sandbox Code Playgroud)
至
private static void setServerURL(final String serverURLXYZ) {
Utility.serverURL = serverURLXYZ;
}
Run Code Online (Sandbox Code Playgroud)
请享用...
吉加尔·帕特尔(Jigar Patel)
归档时间: |
|
查看次数: |
71152 次 |
最近记录: |