Java – Checkstyle: How to Resolve “Hidden Field” Error

checkstyleeclipsejava

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?

Best Answer

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