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

3

6

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.

Shem answered 8/11, 2010 at 10:19 Comment(1)
Mine doesn't complain at all....Luciferase
C
12

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

Charnel answered 8/11, 2010 at 10:44 Comment(2)
Agreed, name shadowing can cause subtle errors, especially if you aren't using the this keyword to prefix any class variables. However, it does seem silly for constructors and setters/gettersPavonine
However: Not doing name shadowing can cause subtle errors too. Just the other way round, like having a method access a field rather than its parameter, if people apply conventions that a parameters have a _p suffix, but then forget this in their implementation.Contrarious
H
1

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

Homeopathist answered 8/11, 2010 at 10:22 Comment(1)
Its a setting that is disabled by default in eclipse. In preferences, Java - compiler - errors/warnings, under "name shadowing and conflicts"Shem
S
1

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!

Stubbed answered 9/1, 2011 at 13:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.