Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of eclipse warning "field declaration hides another field or variable"?

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.

like image 417
Fredrik Avatar asked Nov 08 '10 10:11

Fredrik


People also ask

How do I fix eclipse warnings?

In your Eclipse, go to Window > Preferences > Java > Compiler > Errors/Warnings: Here you could switch any Warning entry to Error. Beware some entries may be hidden by expandable panels, so make sure you expand them all.

How can I see warnings in eclipse?

Click on the small triangle in the upper right corner of the problems view and select "Configure Contents". In that dialog check "Show all items" and uncheck "Use item limits" to show all warnings.


2 Answers

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

like image 136
Qwerky Avatar answered Sep 22 '22 21:09

Qwerky


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

like image 34
Bozho Avatar answered Sep 23 '22 21:09

Bozho