{"id":154415,"date":"2020-09-10T02:16:58","date_gmt":"2020-09-09T18:16:58","guid":{"rendered":"http:\/\/4563.org\/?p=154415"},"modified":"2020-09-10T02:16:58","modified_gmt":"2020-09-09T18:16:58","slug":"inside-the-java-virtual-machine-chapter-8-the-linking-model-resolution-of-constant_fieldref_info-entries%e7%9a%84%e4%b8%80%e4%b8%aa%e9%94%99%e8%af%af","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=154415","title":{"rendered":"\u201cInside the Java Virtual Machine &#8211; Chapter 8 The Linking Model &#8211; Resolution of CONSTANT_Fieldref_info Entries\u201d\u7684\u4e00\u4e2a\u9519\u8bef"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u201cInside the Java Virtual Machine &#8211; Chapter 8 The Linking Model &#8211; Resolution of CONSTANT_Fieldref_info Entries\u201d\u7684\u4e00\u4e2a\u9519\u8bef               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : JasonLaw <\/span>  <span><i><\/i> 2<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5728Inside the Java Virtual Machine &#8211; Chapter 8 The Linking Model &#8211; Resolution of CONSTANT_Fieldref_info Entries\u4e2d\uff0c\u5b83\u8bf4\uff1a<\/p>\n<blockquote>\n<p>If resolution to the CONSTANT_Class_info completes successfully, the virtual machine performs the field lookup process using these steps:<\/p>\n<ol>\n<li>The virtual machine checks the referenced type for a field of the specified name and type. If the virtual machine discovers such a field, that field is the result of the successful field lookup.<\/li>\n<li>Otherwise, the virtual machine checks any interfaces directly implemented or extended by the type, and recursively, any superinterfaces of interfaces directly implemented or extended by the type, for a field of the specified name and type. If the virtual machine discovers such a field, that field is the result of the successful field lookup.<\/li>\n<li>Otherwise, if the type has a direct superclass, the virtual machine checks the type&#8217;s direct superclass, and recursively all the superclasses of the type, for a field of the specified name and type. If the virtual machine discovers such a field, that field is the result of the successful field lookup.<\/li>\n<li>Otherwise, field lookup fails.<\/li>\n<\/ol>\n<\/blockquote>\n<p>\u5173\u4e8e\u7b2c 3 \u70b9\uff0c\u5b83\u8bf4\u201cif the type has a direct superclass, the virtual machine checks the type&#8217;s direct superclass, and recursively all the superclasses of the type, for a field of the specified name and type\u201d\uff0c\u8fd9\u91cc\u9057\u6f0f\u4e86\u201c\u5bf9 superclass \u7684 superinterfaces \u7684\u68c0\u67e5\u201d\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u4f8b\u5b50\u53ef\u4ee5\u8bc1\u660e\uff0cJVM \u4f1a\u68c0\u67e5 superclass \u7684 superinterfaces \u3002<\/p>\n<pre><code>import java.lang.Math;  public class Test {     public static void main(String[] args) {         System.out.println(A.i);     } }  class A extends AParent { }  class AParent implements B { }  interface B {     int i = (int) (Math.random() * 100); } <\/code><\/pre>\n<p><code>javac Test.java<\/code>\u4e4b\u540e\uff0c<code>javap -c Test.class<\/code>\u7684\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n<pre><code>Compiled from \"Test.java\" public class Test {   public Test();     Code:        0: aload_0        1: invokespecial #1                  \/\/ Method java\/lang\/Object.\"&lt;init&gt;\":()V        4: return    public static void main(java.lang.String[]);     Code:        0: getstatic     #2                  \/\/ Field java\/lang\/System.out:Ljava\/io\/PrintStream;        3: getstatic     #3                  \/\/ Field A.i:I        6: invokevirtual #4                  \/\/ Method java\/io\/PrintStream.println:(I)V        9: return } <\/code><\/pre>\n<p>\u8fd0\u884c<code>java Test<\/code>\u6ca1\u6709\u95ee\u9898\uff0c\u53ef\u4ee5\u770b\u51fa JVM \u89e3\u6790\u4e0b\u6807\u4e3a 3 \u7684 constant pool entry \u65f6\uff0c\u6700\u540e\u4f1a\u627e\u5230<code>class A<\/code>\uff0c\u901a\u8fc7<code>class A<\/code>\u4f1a\u627e\u5230<code>class AParent<\/code>\uff0c<strong>\u6700\u540e\u4f1a\u627e<code>class AParent<\/code>\u7684 superinterface <code>interface B<\/code><\/strong>\u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u201cInside the Java &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/154415"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=154415"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/154415\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=154415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=154415"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=154415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}