Sunday, December 09, 2007

How Java handles Method ?

In my previous blog "Performance - Final Keyword ? " One of the blog readers asked question about How Java handles overriding ! So, I decided to make one entry(I guess more than one) for the answer.

Yes overriding tactics in Java is very different from C++ as methods by default in Java can be overridden unlike C++. In C++, the concept of overriding functions are handled by Virtual Table, VTable(This wiki link contains lot of information). Whereas in Java there is some other concept. Before going into the depth, let's see some of the basic things which one should need to know before making hand dirty in overriding concept.

These are the lines related to method invocation. So what the heck is this invokespecial and invokevirtual ? Actually JVM used 4 different kinds of instructions for method invocation those are :

- invokevirtual - This is for instance method like System.out.println("Hello Bloggers!") here.- invokestatic - This is for class methods.- invokespecial - This is for special things. It is used when - call , instance initialization. - super call, when you will call something from super.method - private methods. As private methods can't be overridden so we need to put this in a special category.- invokeinterface - invoking instance method with interface reference(Soon we will see the example)

Now we are very clear that why invokespecial has been used at #1 and #3 whereas invokevirtual at #6. Ok, lets write some code which can see the usages of all four.

good blog friends! Post your article which is also useful for readers and to share information or experiences you have. I will visit your blog again. thank you very much. i love you full http://www.male-sexual.com

i easily love all your posting way, very useful,don't give up as well as keep writing seeing that it just truly worth to look through it,looking forward to look into much of your content articles, have a pleasant day!