Home > Net >  Checkstyle: How to Resolve "[variable name] Hides a field [HiddenField]" Error
Checkstyle: How to Resolve "[variable name] Hides a field [HiddenField]" Error

Time:10-03

I am getting this checkstyle error:

'appInfoFunc' hides a field. [HiddenField]

in the code:

 private long appInfoFunc;
 public String printData(long appInfoFunc) {
   this.appInfoFunc=appInfoFunc;
   if (appInfoFunc== -1) {
     return "N/A";
   }
   return String.valueOf(appInfoFunc);
 }

What could be the reason, and how to resolve it?

CodePudding user response:

Your method parameter has the same name as the field in the class (both are called appInfoFunc). This can be confusing when you use appInfoFunc in your code because you will not immediately know if it is a field or a parameter.

Rename the field or rename the parameter.

CodePudding user response:

The reason is the (identical) naming of "field" and "parameter", the resolution straight-forward: rename one of them (probably better parameter)

To prepend a p to parameter (so pAppInfoFunc) is a common practice:

 private long appInfoFunc;
 public String printData(long pAppInfoFunc) {
   appInfoFunc = pAppInfoFunc;
   if (appInfoFunc == -1) {
     return "N/A";
   }
   return String.valueOf(appInfoFunc);
 }
  • Related