Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

java - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...