• Post Reply Bookmark Topic Watch Topic
  • New Topic

Final  RSS feed

 
Anirudh Vyas
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone explain to me (in english? ), what happens when i mark class/ methods as final (i want to know what is meant by compiler inlining the method or a class).

I have read that this can lead to performance improvements, however this is refuted by many saying that compiled version of class can be different than the loaded version.

If anyone can explain those points that'd be great.

Regards
Vyas, Anirudh
 
Wirianto Djunaidi
Ranch Hand
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marking final on a class will make the class un-inheritable, in other word you can't extend a final class.

Marking final on a method will make the method can not be overriden.

Method inlining is the case where the compiler insert the content of your method where the method call happen. Not all compiler does this. Since java is object oriented language with polymorphism as one of its characteristics, it usually does not inline method because it won't know which method will actually got called during runtime. When you mark a method to be final, then the compiler will know for sure the method can't be overriden by any of the child classes so in theory it might inline the method to improve performance.

Although AFAIK, most java compilers do not do this. If they do, it is more likely done by the JIT and usually it has nothing to do with the method marked final or not. Its more from statistics that the JIT figures out that a specific method get called many times and it determines that it is more efficient to inline the method.
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the class is not final, some subclass may extend it, so that calling a method in the class may not dispatch to the method in the class, but rather to the method in a subclass.

When you declare the class final, there can be no subclass, so the compiler knows the any method call will really get dispatched to the method. This makes it easy to inline the code if needed for performance.

This does not mean it will be inlined, but its a lot easier for the JVM to know that it can be done
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree that making a class final may make it easier for the compiler or (more likely) HotSpot to inline its methods. This may achieve some very modest performance improvement.

But I would argue that you should not do this, except in very unusual circumstances. As with all micro-optimisations, you should generally avoid them and instead concentrate on making a clear, maintainable program.

Declaring a class final is something you should only do when subclassing would break the design. By declaring a class final, you should be saying "subclasses of this class would make no sense".

The only other thing it can validly say is "I have proven that this class is performance-critical and I have proven that declaring it final gives me a performance gain". You shouldn't be saying "I had this vague idea that my code would go faster if I did this".
 
Anirudh Vyas
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter,

Since asking that final question (in which you answered differently or rather cautiously about 'final'), i came across this article :

Final
Is that your Final Answer?
[ April 19, 2008: Message edited by: Anirudh Vyas ]
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Peter that using final as a crutch for performance is a bad idea. Premature optimization... and all that.

I followed the links to the article, and it brought up one of my pet peeves, that Integer is declared final (or was in Java 1 through 3, at which time I stopped looking) without any justification.

There may, perhaps, be a small (mostly theoretical) security issue, but I've had to cut and paste the source code so I could make a "OID (object identifier) class to be type safe, when all it really needed to be was subclass of Integer.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!