I am getting this checkstyle error:
我收到这个checkstyle错误:
'serverURL' hides a field
in this
在这
private static void setServerURL(final String serverURL) {
Utility.serverURL = serverURL;
}
What could be the reason, and how to resolve it?
可能是什么原因,以及如何解决它?
5 个解决方案
#1
38
There is already a variable defined serverURL
which is available to this method (additional to the formal parameter you are accepting). This is called "shadowing".
已经有一个变量定义的serverURL可用于此方法(除了您接受的形式参数之外)。这被称为“阴影”。
I think most Java programmers turn this check off, because it's not really that confusing.
我认为大多数Java程序员都会关闭这个检查,因为它并不是那么令人困惑。
For example, this would trigger the error:
例如,这会触发错误:
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;
}
}
#2
29
I think it is very common in constructors and setters that the set field name is the same as the setter parameter name. This is why i recommend this configuration:
我认为在构造函数和setter中,set字段名称与setter参数名称相同是很常见的。这就是我推荐这种配置的原因:
<module name="HiddenField" >
<property name="ignoreSetter" value="true" />
<property name="ignoreConstructorParameter" value="true" />
</module>
This way the other hidden field cases are still forbidden.
这样,其他隐藏的野外案件仍然是被禁止的。
#3
7
I resolved it by disabling it in eclipse. I was looking for how to do that when I landed on this page. I didn't find the answer in top 10 google query so I had to figure it out the hard way. For someone who is looking for that, here is how I did it:
我通过在eclipse中禁用它来解决它。当我登陆这个页面时,我一直在寻找如何做到这一点。我没有在前10个谷歌查询中找到答案,所以我不得不弄清楚这个问题。对于正在寻找它的人来说,我就是这样做的:
Open
打开
Eclipse>Preferences>Checkstyle
Eclipse的>首选项>的Checkstyle
Find the checkstyle configuration you are using (you might have set that or your are using default in which case its a better idea to create a copy of your own and then edit that). Select that and then click the configure button on the right side. Find the following config in the list:
找到您正在使用的checkstyle配置(您可能已设置或正在使用默认设置,在这种情况下,最好创建自己的副本然后进行编辑)。选择该选项,然后单击右侧的配置按钮。在列表中找到以下配置:
Coding Problems>Hidden Field
编码问题>隐藏字段
Open the configuration (there is button called 'open' in the UI).
打开配置(UI中有一个名为“open”的按钮)。
Unselect 'Parameter Declaration'. Click OK then Click OK and then Click OK.
取消选择“参数声明”。单击OK然后单击OK,然后单击OK。
#4
6
The parameter and the static field have the same name. Just rename one of them. Some people follow a naming convention that prefixes all parameters with p
. Then you would have serverURL
as field name and pServerURL
as parameter name. Or you could simply turn off the check.
参数和静态字段具有相同的名称。只需重命名其中一个。有些人遵循命名约定,该约定用p作为所有参数的前缀。然后,您将serverURL作为字段名称,将pServerURL作为参数名称。或者你可以简单地关掉支票。
#5
3
Just change ur param name in ur method
只需在ur方法中更改ur param名称即可
private static void setServerURL(final String serverURL) {
Utility.serverURL = serverURL;
}
to
至
private static void setServerURL(final String serverURLXYZ) {
Utility.serverURL = serverURLXYZ;
}
Enjoy...
请享用...
Jigar Patel
Jigar Patel
#1
38
There is already a variable defined serverURL
which is available to this method (additional to the formal parameter you are accepting). This is called "shadowing".
已经有一个变量定义的serverURL可用于此方法(除了您接受的形式参数之外)。这被称为“阴影”。
I think most Java programmers turn this check off, because it's not really that confusing.
我认为大多数Java程序员都会关闭这个检查,因为它并不是那么令人困惑。
For example, this would trigger the error:
例如,这会触发错误:
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;
}
}
#2
29
I think it is very common in constructors and setters that the set field name is the same as the setter parameter name. This is why i recommend this configuration:
我认为在构造函数和setter中,set字段名称与setter参数名称相同是很常见的。这就是我推荐这种配置的原因:
<module name="HiddenField" >
<property name="ignoreSetter" value="true" />
<property name="ignoreConstructorParameter" value="true" />
</module>
This way the other hidden field cases are still forbidden.
这样,其他隐藏的野外案件仍然是被禁止的。
#3
7
I resolved it by disabling it in eclipse. I was looking for how to do that when I landed on this page. I didn't find the answer in top 10 google query so I had to figure it out the hard way. For someone who is looking for that, here is how I did it:
我通过在eclipse中禁用它来解决它。当我登陆这个页面时,我一直在寻找如何做到这一点。我没有在前10个谷歌查询中找到答案,所以我不得不弄清楚这个问题。对于正在寻找它的人来说,我就是这样做的:
Open
打开
Eclipse>Preferences>Checkstyle
Eclipse的>首选项>的Checkstyle
Find the checkstyle configuration you are using (you might have set that or your are using default in which case its a better idea to create a copy of your own and then edit that). Select that and then click the configure button on the right side. Find the following config in the list:
找到您正在使用的checkstyle配置(您可能已设置或正在使用默认设置,在这种情况下,最好创建自己的副本然后进行编辑)。选择该选项,然后单击右侧的配置按钮。在列表中找到以下配置:
Coding Problems>Hidden Field
编码问题>隐藏字段
Open the configuration (there is button called 'open' in the UI).
打开配置(UI中有一个名为“open”的按钮)。
Unselect 'Parameter Declaration'. Click OK then Click OK and then Click OK.
取消选择“参数声明”。单击OK然后单击OK,然后单击OK。
#4
6
The parameter and the static field have the same name. Just rename one of them. Some people follow a naming convention that prefixes all parameters with p
. Then you would have serverURL
as field name and pServerURL
as parameter name. Or you could simply turn off the check.
参数和静态字段具有相同的名称。只需重命名其中一个。有些人遵循命名约定,该约定用p作为所有参数的前缀。然后,您将serverURL作为字段名称,将pServerURL作为参数名称。或者你可以简单地关掉支票。
#5
3
Just change ur param name in ur method
只需在ur方法中更改ur param名称即可
private static void setServerURL(final String serverURL) {
Utility.serverURL = serverURL;
}
to
至
private static void setServerURL(final String serverURLXYZ) {
Utility.serverURL = serverURLXYZ;
}
Enjoy...
请享用...
Jigar Patel
Jigar Patel