I am getting this checkstyle error:
'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?
I am getting this checkstyle error:
'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?
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".
I think most Java programmers turn this check off, because it's not really that confusing.
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;
}
}
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:
<module name="HiddenField" >
<property name="ignoreSetter" value="true" />
<property name="ignoreConstructorParameter" value="true" />
</module>
This way the other hidden field cases are still forbidden.
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.
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:
Open
Eclipse>Preferences>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:
Coding Problems>Hidden Field
Open the configuration (there is button called 'open' in the UI).
Unselect 'Parameter Declaration'. Click OK then Click OK and then Click OK.
Just change ur param name in ur method
private static void setServerURL(final String serverURL) {
Utility.serverURL = serverURL;
}
to
private static void setServerURL(final String serverURLXYZ) {
Utility.serverURL = serverURLXYZ;
}
Enjoy...
Jigar Patel
Kinda dumb error, but here is the easy fix I put in my setters to fix it:
was:
public void setName(String name) {
this.name = name;
}
updated:
public void setName(String val) {
this.name = val;
}
I read the documnetation :
Here is the solution:
<module name="HiddenField">
<property name="tokens" value="VARIABLE_DEF"/>
</module>
If already have a HiddentField tag, then add the tokens property.
I had a similar issue, with this fix everything works fine.