2

I am trying to get rid of all the warnings in my code, and tonight I noticed this warning (not compiler warning) from Netbeans. Take this code:

class A {
    public Void method1() {
        return null;
    }

    public final Void method2() {
        return null;
    }
}

on method2() Netbeans says:

Method method2 is declared final

But what's wrong with that? In an implementation of the class, it works just as expected:

class SubA extends A {
    @Override
    public Void method1() {
        return super.method1();
    }

    @Override
    public Void method2() { // <---- this throws an error
        return super.method2();
    }
}

So why is Netbeans complaining?

Note: I know I can turn off the warning (and I know how), but I would like to know what logic is behind it (if any).

ryvantage
  • 13,064
  • 15
  • 63
  • 112
  • 2
    One cannot override a `final` method>>>>> – nIcE cOw Jul 18 '15 at 05:19
  • 1
    Did you read the question? I am aware of that, that's exactly why I want to create a final method. I want to know why Netbeans is complaining about it. – ryvantage Jul 18 '15 at 05:21
  • Check your netbeans settings, it is configured like that to give you warnings. You can disable it. – MJSG Jul 18 '15 at 05:37

3 Answers3

2

In Netbeans 8+:

  1. Goto Tools --> Options
  2. Click on Editor top button
  3. Goto Hints tab.
  4. Make sure Java is selected in the Language combo box.
  5. In the left tree view, expand Class Structure.
  6. 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:

  1. final hinders or prevents reuse when used to mark classes or methods.

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

  3. 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).

Community
  • 1
  • 1
sstan
  • 35,425
  • 6
  • 48
  • 66
  • Did you read the note at the end of the question? I preempted this answer by saying I know I can (and I know how) to disable it. In fact, I ENABLED it on purpose because I want to be bothered by things that should bother me. My question is "why should this bother me?" – ryvantage Jul 18 '15 at 13:37
  • 1
    I'll admit that I missed that final note :) But to be fair, your question was `So why is Netbeans complaining?`, not `why should this bother me?`. In any case, I expanded my answer to cover both questions if you're still interested. – sstan Jul 18 '15 at 14:21
2

It's just a caution.

It's just saying that you have declared final method, but beware in future that someone shouldn't override this method, otherwise you'll get an error, because overriding final method(s)/class(es) is/are not allowed.

If you've design like that, and you think that it's a good design, then don't bother this warning; otherwise, change your design.

Muhammad Imran
  • 734
  • 7
  • 21
1

Netbeans isn't "complaining about it". It is just drawing your attention towards that.

The reason for this as stated here under Class Structure is,

Final method [Disabled by default]

Reports any instances of methods being declared final. Some coding standards discourage final classes.

Since NetBeans 6.9

Do have a look at this article which was suggested by Mathias Begert in the comments section.

Community
  • 1
  • 1
Ghazanfar
  • 1,419
  • 13
  • 21
  • 2
    Just an interesting addition about the Why: http://www.ibm.com/developerworks/java/library/j-jtp1029/j-jtp1029-pdf.pdf – Mathias Begert Jul 18 '15 at 06:13
  • @MathiasBegert Very nice article ! Thank you ! – Ghazanfar Jul 18 '15 at 06:22
  • Yes, interesting article. It seems `final` is overused, but my situation calls for a method that should definitely not be changed by any subclasses so I think it's legit. That article is definitely the answer I was looking for. – ryvantage Jul 18 '15 at 13:44