Checkstyle: How to Resolve "Hidden Field" Error

97,034

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.

Solution 4

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

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

Share:
97,034
Romi
Author by

Romi

Updated on July 11, 2020

Comments

  • Romi
    Romi almost 4 years

    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?

  • Romi
    Romi over 12 years
    As you see method is static, i can not use this
  • James Oravec
    James Oravec about 11 years
    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.
  • Hannes M
    Hannes M almost 11 years
    How about fixing the code than to turn of the checker? see Cory Kendall's answer
  • T A
    T A almost 11 years
    @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.
  • IgorGanapolsky
    IgorGanapolsky about 10 years
    @CoryKendall What do you propose is the solution to this problem?
  • Cory Kendall
    Cory Kendall over 7 years
    The solution is to turn off the check, or chance the name of one of the bars to something else.
  • wachr
    wachr over 6 years
    This answer could be improved by specifying how to turn off that checkstyle.
  • Gaurav
    Gaurav almost 5 years
    still not working with above properties in my checkstyle xml file.
  • Tucker
    Tucker almost 3 years
    ahhhhhhhhhhhhhh!