Usage of eclipse warning “field declaration hides

2019-01-18 15:43发布

问题:

Eclipse has a java compiler setting called "field declaration hides another field or variable" that can be set to warning/error.

How important is this warning in your opinion?

What is a good standard way to handle this problem?

Code example of where this happens:

public class Test {
   private String caption = null;

   public Test(String caption) { // here
     this.caption = caption;
   }
}

I've seen solutions where the field is renamed, i.e "fCaption", but that would cause the automatic getters/setters that can be genereated to have odd names (getfCaption()). Not unreadable, but ugly...

Edit: Oh yea, there is the possibility to rename the method signature Test(String caption_) or something similar, but that would end up in the javadoc looking weird.

回答1:

This is a very useful option in my opinion and should be enabled to show a compiler warning. There is an option (in my version at least Eclipse 3.5.2, Java EE feature 1.2.2) to further enable/disable it within constructors and getters/setters to prevent false positives.



回答2:

I'd say that you just disable this warning - it seems no use in your convention. And no wonder it is ignored by default.



回答3:

I keep these set to "Error". If a class and its parent both have a field of the same name I don't want to lose any of my time trying to figure out why I seem to be assigning a value to the field yet it never seems to change!