Checkstyle: How to Resolve "Hidden Field" Error

Romi picture Romi · Oct 15, 2011 · Viewed 86.8k times · Source

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?

Answer

Cory Kendall picture Cory Kendall · Oct 15, 2011

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