Jvm method Invocation (invokevirtual)

Source: Internet
Author: User

When Java code is running, the calls between methods are the most frequent, so what do the calls between these methods do on the underlying virtual machine? Now let us uncover the veil of mystery.

The JVM calls the method with five instructions, namely invokestatic,invokespecial,invokevirtual,invokeinterface,invokedynamic. Invokestatic is used to invoke a static method; Invokespecial is used to invoke a private method, a parent class method (super.), a class constructor method, a Invokeinterface call interface method, and a invokedynamic method to execute dynamically. Invokevirtual calls all virtual methods, that is, all but the above methods are called with invokevirtual.

This article mainly explains the invocation of the invokevirtual method, as an example.

class father{public void FMe () {System.out.println ("fMe"); FMe1 ();// Invovespecial call SYSTEM.OUT.PRINTLN (this); This.fme1 ()//invovespecial call}private void FMe1 () {System.out.println (" FMe1 ");}} Class Son extends father{
public void FMe1 () {
        system.out.println ("SMe1");
    }} public class Thistest{public static void Main (string[] args) {Father test = new Son () ;//At compile time to the FME () of the parent class China, at run time because it is a invokevirtual call, so test will become the actual type son, if there is fMe () in son, call son's own, if not the parent class}}

There is a public method in the parent class Father FME () and a private Method FMe1 (), the subclass does not overwrite its method, in the test class thistest Father test = new Son (), and the Fme (), and then in the FME () Call your own private method, FMe1 (). In several method calls as above, Test.fme () is a invokevirtual call, and at compile time it points to the parent class, China's FMe (), at run time because it is a invokevirtual call, so test will become the actual type son, if there is fMe () in Son, The parent class is called when the parent class is called, and FMe1 () in the parent class is the invovespecial call. question (also for the purpose of writing this article): The above is for son, since it is invovespecial call, then it should be called Son's fMe1 () to Ah, why is called the parent class FMe1(). (Although the feeling is very unreasonable, obviously is the private method, must have only such call)

  The following gives my own understanding, do not know right!

When this is called with this, I think that during compilation, this represents the Father class, not the son class, and because of this, in the case of a invovespecial bytecode call, the compiler determines the pointer to the parent class FMe1 () method. Instead of a subclass method. To determine what I said, I used two ways to verify: 1 is the dynamic hint with MyEclipse, 2 is to change the fMe1 () method to public, This is when the byte code refers to the invokevirtual call is not changed during the run of this is the actual type of son class, that is, is not to call the subclass of the FMe1 () method.

1: I used the hint key in myeclipse to get the following, can see only the parent class of two methods, and there is no subclass son method.

2. After I change the FMe1 () method to public, it is a method that is called a subclass.

Class Father{public void FMe () {System.out.println ("fMe"); this.fme1 ();//invovespecial call System.out.println (this); This.fme1 ();//invovespecial call}public void FMe1 () {System.out.println ("FMe1");}} Class Son extends Father{public void FMe1 () {        System.out.println ("SMe1");}    } public class Thistest{public static void Main (string[] args) {Father test = new Son (); Test.fme ();//compile-time point to the parent class of FMe () in China, at run time due to is a invokevirtual call, so test will become the actual type son, if there is Fme () in son, call son's own, if not the parent class}}

This is to see invokevirtual think of, do not know right, if who can untie my this doubt will be very grateful.

  

JVM method Invocation (invokevirtual)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.