In Netbeans 8+:
- Goto
Tools --> Options
- Click on
Editor
top button
- Goto
Hints
tab.
- Make sure
Java
is selected in the Language
combo box.
- In the left tree view, expand
Class Structure
.
- Check if
Final method
is checked.
Your warning is very likely happening because the above configuration is enabled, which should not be by default. Notice what Netbeans says it does when you enable the Final method
warning:
Reports any instances of methods being declared final. Some coding standards discourage final classes.
So it really doesn't try to be smart about it. It just reports them all as warnings because some people consider final methods in general to be bad practice.
EDIT: Comment Follow-up
Your original question was:
So why is Netbeans complaining?
And the answer to that is simply: because you asked it to complain any time it finds a method declared as final
.
but I would like to know what logic is behind it (if any).
There is no logic the way you are implying there is. It doesn't try to analyze when a specific use of final
makes sense and when it doesn't to then warn you about it. It just blindly honors your request of warning you of all uses of the final
modifier on a method. It's then up to you to determine whether a warning needs to be acted on. The burden of analyzing whether the use of the final
modifier in a specific case is legitimate is left to you.
Now, in your comment you now say that your question is:
why should this bother me?
... which is not the same question, and was not stated anywhere. But since you now ask, I'll say that there is no clear-cut answer to this question, and is largely opinion-based. That would be why the warning is not enabled by default in Netbeans, because some people are not bothered by final
methods, and think they have legitimate uses.
Some people argue that using final
is evil. For example, the following article says:
Case for evilness:
final
hinders or prevents reuse when used to mark classes or methods.
Marking methods as final
is unlikely improve the runtime efficiency of the code since the JitCompiler? already has information about which methods have been overridden and which are leaves.
It's one more word somebody reading the code has to scan over. Naming constants in uppercase is very common so final
is redundant.
Those same people usually feel that methods and classes should be left "unlocked" by default, in case someone decides that they need to override part of a class by overriding a method in the future.
In contrast, others (like me) feel that the opposite is safer. I prefer when my class/methods cannot be overridden by default, and that I have to explicitly enable which parts of a class can be safely overridden, if any. This is the default adopted by languages like C#, where, by default, you cannot override methods. If you want to enable overriding, you have to do it explicitly my marking the base class' method virtual
.
The question of which default is better (Java's or C#'s) is the subject of many hot debates. See here and here for examples.
So, which side of the debate you are on:
- the side that thinks that classes/methods should be left open for inheritance/overriding just in case, or
- the side that thinks that classes/methods should be locked down by default, and opened for inheritance/overriding only when necessary
... will determine whether you will care about having Netbeans warn you about final
methods (and final
classes for that matter).