I had a misconception about final keyword that, it will speed up method calls if methods are marked as final. My thought was that, this will allow javac compiler to do static binding instead of leaving it to late binding at runtime. May be I might have got this point from some java performace tip articles. But the truth is that, java uses late binding even when methods are marked as final.
class test1 {
public void m1() {
System.out.println(m2());
}
public int m2() {
return 5;
}
}
class test2 extends test1 {
public int m2() {
return 10;
}
public static void main(String args[]) {
test1 t = new test2();
t.m1();
}
}
run “java test2″. This will print 10 as expected.
Now change m1() of test1 to final and compile test1.java alone.
This time when you try to run test2, java throws run time exception, java.lang.VerifyError.
This exercise shows/confirms that java does late binding even for methods marked as final.
So the conclusion is : Poor javac (Java compiler) can’t do static binding even for final methods. There is no way for the compiler to find if a sub class has already overridden a method, when it was not final.





