开发者

Why doesn't default Eclipse complain about unused method parameters?

I would like to know why Eclipse doesn't by default register an error or warning about a method like this:

public void setX(int x) {
    // empty method body
}

You can turn a warning on for this that reads "The parameter x is never read", in Preferences>Java>Compiler>Errors/Warnings>Unnecessary Code. You can set this warning so that it ignores overriding and implement开发者_Go百科ing methods.

I can only think of one reason why you may want to ignore the situation in the code I pasted: you may want to call a particular version of an overloaded method by using an arbitrary-value argument of a particular type. It seems that would be very bad programming practice though. Is there some better reason for leaving this warning off by default?


One situation where it can be useful is if you're writing deliberate no-op methods in a base class, which can be overridden by derived classes. Think of things like MouseAdapter for example.

The problem is there's no way of indicating in the language "yes, this is deliberately a no-op"... the compiler either has to take comments into account, or you've got to use @SuppressWarnings etc.


Link to filters settings

In the filters, you can set it to "On selected element only", and then you get rid of alot of warnings. I use that setting, and it's very helpful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜