1

Is there an advantage to declaring a private method as "final" in Java?

If my understanding is correct, the "final" Modifier makes sure I cannot override a method in a subclass and so this may make this method more efficient during runtime. Some literature says it can be inlined if it is final for example which may make the method call faster. Also, the Java runtime won't have to go look for other method in the inheritance hierarchy if it is declared final.

However, private functions cannot be overriden, so are they implicitly "final" as well? Is there a difference between the following two declarations:

private void myFun()

and

private final void myFun();
mohsaied
  • 2,066
  • 1
  • 14
  • 25

3 Answers3

5

Subclasses may not override private methods by design. Furthermore, the final keyword tells the compiler that subclasses may not override a method regardless of its access level.

Since private already implies that a subclass may not override a method, declaring a private method to be final is redundant. Making the declaration won't cause problems, but it won't accomplish anything either, since privates are automatically considered final.

All compilers will treat private methods as final. The compiler will not allow any private method to be overridden. Likewise, all compilers will prevent subclasses from overriding final methods.

Update

In your question you're saying

the "final" Modifier makes sure I cannot override a method in a subclass and so makes this method more efficient during runtime

This is quite a questionable statement. If you declare your private method final, the optimization will depend on the compiler you are using and it's settings. That means NO. No, it's not necessarily true that if you do declare your method private and final, or just final, it would run faster.

Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
  • Java doesn't have compilers with different optimization settings. They leave optimization up to the JVM. – Boann Dec 07 '14 at 22:44
  • @Boann, how about `javac`, `GCJ` or `ECJ`? – Kevin Kopf Dec 07 '14 at 22:45
  • @Boann http://stackoverflow.com/questions/5981460/optimization-by-java-compiler – Kevin Kopf Dec 07 '14 at 22:46
  • @Boann, GCJ docs: `When the optimization level is greater or equal to "-O2", gcj will try to optimize the way calls into the runtime are made to initialize static classes upon their first use (this optimization isn't carried out if "-C" was specified.) When compiling to native code, "-fno-optimize-static-class-initialization" will turn this optimization off, regardless of the optimization level in use.` – Kevin Kopf Dec 07 '14 at 22:47
4

No, the final keyword is unnecessary for private methods. Per definition, there is no difference between private and private final.
For more information, see 'final methods' in the Java Language Specification.

Boann
  • 48,794
  • 16
  • 117
  • 146
Gren
  • 1,850
  • 1
  • 11
  • 16
1

These are the different meanings and advantages of final in Java :

  • The final keyword on a primitive type variable makes it a constant impossible to reassign. the interest is obvious.

  • The final keyword on an object variable prevents its reference from being changed.

  • The final keyword on a method prevents it from being overrided in a subclass. It is useful to protect the functionality of a critical method the superclass while letting the subclasses see it.

  • The final keyword on a class prevents it from being extended by another class. It is most of the time a bad idea but many base classes of the standard library are like this (for example java.lang.Math, java.lang.String, java.lang.Integer...)

As you can see, declaring some method both private and final is equivalent to declaring it as simply private because in both cases, subclass won't be able to override it.

However, there is a difference for a variable. Indeed, declaring it private will only hide it to other classes whereas adding final will also prevents its reference from being changed in the code of the current class (an will force the constructors to initialize it).

Dici
  • 25,226
  • 7
  • 41
  • 82
  • Then what is the point of having a final private method? It is not like subclasses can see private (without final!) methods – engineercoding Dec 07 '14 at 22:31
  • 1
    Only point 3 is relevant to the question. – Adam Dec 07 '14 at 22:31
  • 1
    @Adam But it is potentially helpful to future readers to show that `final` has different meanings in different contexts in java. – Matt Coubrough Dec 07 '14 at 22:33
  • @MattCoubrough but yet you have not really answered the question; is there any point to adding final to private methods? – engineercoding Dec 07 '14 at 22:33
  • @codequestioneer I did not mention such methods, at the contrary, I said final enabled to let a method being seen by the subclasses yet not risking them to alter its behaviour – Dici Dec 07 '14 at 22:34
  • @MattCoubrough Really?? if they happened to stumble on a question titled, "Is there an advantage to declaring a private method “final” in Java?" – Adam Dec 07 '14 at 22:34
  • @Dici still unclear if and why I would want to use "private final" on a method. Perhaps it doesn't make any difference and it is just a redundancy that the language permits? – mohsaied Dec 07 '14 at 22:35
  • @Adam Hum I just saw "private" in the title. – Dici Dec 07 '14 at 22:35
  • @codequestioneer I haven't answered the question because it is a duplicate and I voted up the comment flagging it as such: http://stackoverflow.com/a/1985183/3651800 – Matt Coubrough Dec 07 '14 at 22:35
  • @Dici Apologies, I believe the question has been rewritten since you started answering it. – Adam Dec 07 '14 at 22:36
  • @Adam We cannot predict what edits the poster will make to the title of his question after he posts it – Matt Coubrough Dec 07 '14 at 22:38
  • @MattCoubrough I havent seen it else, but you have your point. No worries :) – engineercoding Dec 07 '14 at 22:39
  • I edited my answer so that it answers better to the edited question (I hope) – Dici Dec 07 '14 at 22:42