75

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?

eckes
  • 10,103
  • 1
  • 59
  • 71
Romi
  • 4,833
  • 28
  • 81
  • 113

7 Answers7

63

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;
  }
}
Cory Kendall
  • 7,195
  • 8
  • 37
  • 64
53

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.

WonderCsabo
  • 11,947
  • 13
  • 63
  • 105
11

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.

barfuin
  • 16,865
  • 10
  • 85
  • 132
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:

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.

T A
  • 566
  • 5
  • 13
  • For some reason my check style had multiple "hidden field" options under "Coding Problems". I disabled all of them then only enabled one and followed the directions above and it worked. Wanted to do a plug for clarification in case others have the same problem. – James Oravec Apr 11 '13 at 14:26
  • How about fixing the code than to turn of the checker? see Cory Kendall's answer – Hannes M Jul 05 '13 at 06:58
  • @Hannes Sure. That is exactly the solution **checkstyle** is suggesting to the user. However people landing on this page are looking for ways to make checkstyle stop warning for that. – T A Jul 07 '13 at 23:03
6

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

user3550483
  • 101
  • 1
  • 1
0

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;
}
Forrest
  • 2,968
  • 1
  • 27
  • 18
0

I read the documnetation :

https://checkstyle.sourceforge.io/apidocs/com/puppycrawl/tools/checkstyle/checks/coding/HiddenFieldCheck.html

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.

ppuskar
  • 773
  • 6
  • 9