Checkstyle: How to Resolve "Hidden Field" Error

Solution 1:

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;
  }
}

Solution 2:

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.

Solution 3:

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.