Checkstyle: How to Resolve "Hidden Field" Error

JavaEclipseCheckstyle

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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 - Java

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 - Java

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.

Solution 4 - Java

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.

Solution 5 - Java

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

Solution 6 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRomiView Question on Stackoverflow
Solution 1 - JavaCory KendallView Answer on Stackoverflow
Solution 2 - JavaWonderCsaboView Answer on Stackoverflow
Solution 3 - JavabarfuinView Answer on Stackoverflow
Solution 4 - JavaT AView Answer on Stackoverflow
Solution 5 - Javauser3550483View Answer on Stackoverflow
Solution 6 - JavaForrestView Answer on Stackoverflow