LendKey

Wednesday, December 24, 2008

Why do we do final in Java?

Noticed lots of nice opensource code and Java style checker suggested to use final and static as much as possible.
I kinda understand why to use static, especially for methods, but not sure why final will help.
After a bit of google, I found the following claim at:

http://www.javaperformancetuning.com/tips/final.shtml#REF1

From: http://www.protomatter.com/nate/java-optimization/
Various tips. (Page last updated 1999?, Added 2000-10-23, Author Nate Sammons, Publisher Sammons). Tips:

Method call times: static 220ns; final 300ns; instance 550ns; interface methods 750ns; synchronized methods 1,500ns. [But times vary enormously depending on the VM and context].
Use static final methods where possible. [And do functional programming too ;-)]


Since the note was added around 1999 and 2000, I'm wondering it still stand true or not.

Now matter what, I will try my best to apply this policy to my code. But if anyone can explain why to me, it will make me feel much better.

Thanks!

5 comments:

Gili Nachum said...

Interesting! I believe this to be the effect of early-binding (final methods) Vs late-binding (non final method).
I myself don't think I'll choose to use this final policy as my default coding policy when writing new code, as I believe that OO design considerations should come first. I'm also one of those whom believe that low level performance tuning yields the best investment returns, if applied after the code is already written, and analyzed using a profiler.
On early/late binding see more details here and here.
I also invite you to come around and check my java tuning web site :-)

Li Ma said...

Hi Gili,
Thanks for your comment. After reading those articles you suggested I kinda agree that first of all, those tricks do not help performance improvement that much, meaning not worth the effort, unless I'm desperate; Secondly, keeping OO concept and maintainability of code is more important than possible gain of performance here.

Thank you again for your advice!
PS: Just curious, I just posted my BLOG a day before, how did you find it so fast? Am I that popular ;)

Cheers!

Gili Nachum said...

Easy, I'm subscribed to your RSS/Atom feed! I probably visited your blog by chance and like what I read, so I decided to come again. Guess that means your gaining some popularity at least ;)
You can use something like Feedburner to monitor you blog popularity (or the blogger site might have a tool of its own). For example, using Feedburner helped me realized how *unpopular* my site is, as I currently have only 5 RSS subscribers (one of them being myself). hehehe ;)
I like articles that pose questions in the end and not only dictates answers.

Anonymous said...

I don't know the reasoning for using final prior to JSR 133 but I do know why everybody should use final in JDK 1.5 and later.

Simply: Thread safety in the context of visibility. It's quite shocking to see how "flawed" the Java Memory Model was (and still is when it comes to Constructors - IMHO).

http://www.ibm.com/developerworks/java/library/j-jtp02244.html

http://www.ibm.com/developerworks/library/j-jtp03304

I highly recommend reading the second article!

Li Ma said...

Roger,

I read the two URLs you suggested. Good reading!
It explained Java memory model, especially on synchronized and voletile keywords. But I didn't see anything specific about final. Did I miss anything here?

Thanks!