Usage of eclipse warning “field declaration hides

2019-01-18 15:24发布

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.

3条回答
三岁会撩人
2楼-- · 2019-01-18 15:47

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.

eclipse compiler settings

查看更多
放荡不羁爱自由
3楼-- · 2019-01-18 15:47

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

查看更多
Fickle 薄情
4楼-- · 2019-01-18 16:06

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!

查看更多
登录 后发表回答