IT Community - Software Programming, Web Development and Technical Support

whats the use of final method

This is a discussion on whats the use of final method within the Java Programming forums, part of the Software Development category; hi, why there is need of final methods..declaring method as final improve application security?..if so how......


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > Java Programming

Register FAQ Members List Calendar Mark Forums Read
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 08-23-2007, 12:46 AM
amansundar amansundar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 325
amansundar is on a distinguished road
Default whats the use of final method

hi,
why there is need of final methods..declaring method as final improve
application security?..if so how...
__________________
cheers
Aman
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-26-2007, 11:15 PM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Wink Re: whats the use of final method

There are typically two reasons to make a method final, performance and design.

When a method is final, it may be inlined. Before HotSpot compiling (JDK 1.1.x), these methods were usually inlined at compile time, whereas with HotSpot they are inlined at runtime, unless the compiler can guarantee that the inlined method will always be compiled together with the code that uses it.


Code:
// somebody else's class
public class A {
  public static final void f() {
    System.out.println("A's f()");
  }
}

// our class
public class B {
  public void g() {
    A.f();
  }
}
In the past, at compile time our class would be turned into: 
// compiled class
public class B {
  public void g() {
    System.out.println("A's f()");
  }
}
The effect of this was that we had to make one less method call, and since method calls produce extra overhead, we saved some clock cycles. The disadvantage of this, made clear to me by an old (ok, experienced) COBOL programmer during one of my courses, was that whenever somebody else's class changed we would have to remember to recompile our class!

In JDK 1.[234].x with HotSpot(tm), Sun changed this so that the methods were no longer inlined at compile time, but rather by the HotSpot compiler at run time, IFF the performance measurements suggested that it would improve the overall performance of our code.

There are quite a few factors which will affect whether a method will be inlined or not, and we cannot assume that just because we make something final that it will definitely be inlined. As we saw in newsletter 21, it is a good idea anyway to always recompile all your code when you get a new version of someone else's library, so this is not necessarily a reason to NOT use final methods.

When you make a method final, no-one else will be able to override it again. You thus limit extensibility of your code by choosing to make the method or, even worse, your class final. I have been utterly frustrated in the past when I wanted to extend code where the developer had tried to add optimizations in the form of final. I thus never make a method or class final unless I specifically want to stop do others from overriding them.

So, when do I use final methods? If I have performance values that prove that final makes a difference then I would consider using it for performance reasons, otherwise I would only ever use it for design reasons.
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-10-2007, 03:52 AM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 194
leoraja8 is on a distinguished road
Post Re: whats the use of final method

Contrary to the implication of many tips, methods declared as final cannot be safely inlined by the compiler, because the method could have a non-final declaration at runtime.

To see why, suppose the compiler looks at class A and subclass B, and sub-subclass C and sees a final method in A which it inlines into C. But then at runtime the versions loaded for A and B are different and the method is not final in A, and overridden in B. Then C uses the incorrectly inlined version. This was a bug in some of the early compilers when used with the -O option. So tips suggesting using final and the -O option are not valid tips.

On the other hand, final methods can be inlined after being loaded into the JVM, because at that point the JVM knows definitely that the method is final. So compilers that operate after class loading, such as JIT compilers, can take advantage of final methods. Consequently, methods declared final could have some performance benefit.

Generally, I wouldn't go out of my way to declare a method or class final purely for performance reasons. Only after you've definitely identified a performance problem is this even worth considering.

On the other hand, final variables, especially static final variables, are well worth using as standard.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 09-10-2007, 04:03 AM
amansundar amansundar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 325
amansundar is on a distinguished road
Default Re: whats the use of final method

hi leoraja,
well you say performance of the method wil be maintained but let me know what about the speed of executing the method declared in final compared to ordinary method declaration.........
__________________
cheers
Aman
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 09-14-2007, 03:09 AM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 194
leoraja8 is on a distinguished road
Post Re: whats the use of final method

Since Java is an interpreted language, any programs written in Java, after being converted into Java class files, are interpreted by a Java virtual machine (JVM). In order to improve performance, many JVMs may compile Java classes into platform-specific binary code after they are loaded into the JVM. Then, instead of being interpreted, Java classes are executed in their compiled native code format, similar to programs written in other languages, such as C, C++, etc. Such just-in-time (JIT) compilation of Java programs can significantly improve the speed of execution of Java programs.

A high performance JIT compiler is the key to a high performance Java implementation, and one of the most important actions of a high performance JIT compiler is optimizing method invocation. Because Java is an object-oriented language, methods and method invocation are prevalent in typical Java programs. In Java, there are three kinds of method invocations: (1) static method invocation, also known as class method invocation;
(2) interface method invocation; and
(3) instance method invocation.

An instance invocation can be a non-virtual method invocation (only for private methods and object instance initialization methods) or a virtual method invocation. While static method invocations and non-virtual instance method invocations are easier to optimize due to their static nature, interface method invocations and virtual instance method invocations are the most prevalent in Java. Therefore, improving virtual and interface method invocation is very important for the overall performance of a JIT compiler.

In Java, a virtual method can be declared final. A final method can not be overridden by any subclass, which prevents malicious modification of trusted and verified code and ensures consistency. Hence, invocations of final methods can be more easily optimized by the JIT compiler than those of regular virtual methods.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.discussweb.com/java-programming/3445-whats-use-final-method.html
Posted By For Type Date
DiscussWeb IT Community - Technical Support and Technology Discussions This thread Refback 08-27-2007 02:14 AM

Similar Threads
Thread Thread Starter Forum Replies Last Post
Final methods ragavraj PHP Programming 1 12-06-2007 06:35 AM
Can main method be declared final? leoraja8 Java Programming 1 08-28-2007 01:24 AM
Difference between static and final in java leoraja8 Java Programming 2 07-20-2007 06:46 AM
Whats an assembly? prasath ASP and ASP.NET Programming 1 07-19-2007 02:20 AM
Whats your text? Font , Size etc killerkev06 The Lounge 4 04-06-2007 10:16 AM


All times are GMT -7. The time now is 10:55 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0