Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkstyle: How to Resolve "Hidden Field" Error

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?

like image 434
Romi Avatar asked Oct 15 '11 05:10

Romi


2 Answers

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;   } } 
like image 91
Cory Kendall Avatar answered Sep 19 '22 01:09

Cory Kendall


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.

like image 28
WonderCsabo Avatar answered Sep 17 '22 01:09

WonderCsabo