Issues raised by the This keyword in Java

Source: Internet
Author: User

A long time ago there was a question that had recently been re-searched the JVM's books before it was finally an epiphany. The problem is as follows:

The problem of being forgotten by oneself
1  PackageOrg.hanyan.test.testClass;2 3  Public classT3aboutthis {4      Public Static voidMain (string[] args) {5         NewSubT3 (). Func ();6SuperT3 s =NewSuperT3 ();7SYSTEM.OUT.PRINTLN (SinstanceofSUBT3);//false8     }9 }Ten  One classSuperT3 { A      PublicString name = "Jack"; -      -     protected voidfunc () { theSystem.out.println ( This. name);//Jack -System.out.println ( This instanceofSUPERT3);//true -System.out.println ( This instanceofSUBT3);//true -     } + } - classSubT3extendsSuperT3 { +      PublicString name = "Peter"; A}

Never understand, the above code in the 17th line why print out true.

JAVAP printed the SuperT3 class compiled bytecode directives as follows:

The 13th, 23-byte instructions are the same reference variable, which we can see in the local variable table is LORG/HANYAN/TEST/TESTCLASS/SUPERT3,

It's not clear why the 14-, 24-byte instanceof instruction returns the same result: true

Currently suspected to be in operation, the JVM replaces the local variable this. But this begs the question, why is this in this.name not replaced? What is the replacement rule?

I think it's a fairly accurate answer.

In fact, there are two points to explain this phenomenon:

1. Why does the 17 line print true?

This is the performance of the dynamic discovery process of the virtual machine bytecode instruction Invokevirtual.

13, 23 bytes of instruction parameters are the same reference lorg/hanyan/test/testclass/supert3, here I understand not deep, in fact, the reference here is "symbolic reference", refers to a SuperT3 class constant pool of some contant_ The symbol reference for the Methodref_info type. However, when the virtual machine execution engine executes this bytecode instruction at run time, it first finds the actual type of the recipient (caller) of the instruction, and then determines where the true "direct reference" is pointing. This parsing operation implements the dynamic allocation of methods, which is what we often call "method rewriting". (Refer to the dynamic parsing of Java for the explanation of "symbolic reference" to "direct reference"). So the recipient of the 13, 23 bytecode instruction is actually SubT3, which returns True.

Then it leads to the second point of concern, the 15 behavior of the code what is "Jack"?

2.15 behavior of the code what is "Jack"?

Virtual machine bytecode directive Getfeild, unlike invokevirtual, there is no dynamic discovery process. Therefore, the parameter of the Getfeild bytecode directive is a "symbolic reference" to the constant pool of the current class, at which point the "symbol reference" is converted to "direct reference" during the class loading process, meaning the value of the variable name for the current class, so return "Jack".


At this point, the whole explanation is complete.

In summary, the This keyword is only the accessor of the first parameter of the method (the receiver of the method) in the compiled bytecode file. The whole issue should focus on how virtual machines execute these bytecode directives.

Issues raised by the This keyword in Java

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.