java - netbeans warning: Method is declared final -
i trying rid of warnings in code, , tonight noticed warning (not compiler warning) netbeans. take code:
class { public void method1() { return null; } public final void method2() { return null; } }
on method2()
netbeans says:
method method2 declared final
but what's wrong that? in implementation of class, works expected:
class suba extends { @override public void method1() { return super.method1(); } @override public void method2() { // <---- throws error return super.method2(); } }
so why netbeans complaining?
note: know can turn off warning (and know how), know logic behind (if any).
in netbeans 8+:
- goto
tools --> options
- click on
editor
top button - goto
hints
tab. - make sure
java
selected inlanguage
combo box. - in left tree view, expand
class structure
. - check if
final method
checked.
your warning happening because above configuration enabled, should not default. notice netbeans says when enable final method
warning:
reports instances of methods being declared final. coding standards discourage final classes.
so doesn't try smart it. reports them warnings because people consider final methods in general bad practice.
edit: comment follow-up
your original question was:
so why netbeans complaining?
and answer simply: because you asked complain any time finds method declared final
.
but know logic behind (if any).
there no logic way implying there is. doesn't try analyze when specific use of final
makes sense , when doesn't warn it. blindly honors request of warning of all uses of final
modifier on method. it's determine whether warning needs acted on. burden of analyzing whether use of final
modifier in specific case legitimate left you.
now, in comment question is:
why should bother me?
... not same question, , not stated anywhere. since ask, i'll there no clear-cut answer question, , largely opinion-based. why warning not enabled default in netbeans, because people not bothered final
methods, , think have legitimate uses.
some people argue using final
evil. example, following article says:
case evilness:
final
hinders or prevents reuse when used mark classes or methods.marking methods
final
unlikely improve runtime efficiency of code since jitcompiler? has information methods have been overridden , leaves.it's 1 more word reading code has scan over. naming constants in uppercase common
final
redundant.
those same people feel methods , classes should left "unlocked" default, in case decides need override part of class overriding method in future.
in contrast, others (like me) feel opposite safer. prefer when class/methods cannot overridden default, , have explicitly enable parts of class can safely overridden, if any. default adopted languages c#, where, default, cannot override methods. if want enable overriding, have explicitly marking base class' method virtual
.
the question of default better (java's or c#'s) subject of many hot debates. see here , here examples.
so, side of debate on:
- the side thinks classes/methods should left open inheritance/overriding in case, or
- the side thinks classes/methods should locked down default, , opened inheritance/overriding when necessary
... determine whether care having netbeans warn final
methods (and final
classes matter).
Comments
Post a Comment