{"id":152867,"date":"2020-09-07T22:23:49","date_gmt":"2020-09-07T14:23:49","guid":{"rendered":"http:\/\/4563.org\/?p=152867"},"modified":"2020-09-07T22:23:49","modified_gmt":"2020-09-07T14:23:49","slug":"%e5%85%b3%e4%ba%8einside-the-java-virtual-machine-chapter-7-the-lifetime-of-a-type-initialization%e7%9a%84%e7%96%91%e9%97%ae","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=152867","title":{"rendered":"\u5173\u4e8e\u201cInside the Java Virtual Machine &#8211; Chapter 7 The Lifetime of a Type &#8211; Initialization\u201d\u7684\u7591\u95ee"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5173\u4e8e\u201cInside the Java Virtual Machine &#8211; Chapter 7 The Lifetime of a Type &#8211; Initialization\u201d\u7684\u7591\u95ee               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : JasonLaw <\/span>  <span><i><\/i> 0<\/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 7 The Lifetime of a Type &#8211; Initialization\u4e2d\uff0c\u6709\u8fd9\u4e48\u4e00\u6bb5\uff1a<\/p>\n<blockquote>\n<p>A use of a non-constant static field is an active use of only the class or interface that actually declares the field. For example, a field declared in a class may be referred to via a subclass. A field declared in an interface may be referred to via a subinterface or class that implements the interface. These are passive uses of the subclass, subinterface, or class that implements the interface&#8211;uses that won&#8217;t trigger their initialization. They are an active use only of the class or interface in which the field is actually declared. Here&#8217;s an example that illustrates this principle:<\/p>\n<pre><code>\/\/ On CD-ROM in file classlife\/ex2\/NewParent.java class NewParent {      static int hoursOfSleep = (int) (Math.random() * 3.0);      static {         System.out.println(\"NewParent was initialized.\");     } }  \/\/ On CD-ROM in file classlife\/ex2\/NewbornBaby.java class NewbornBaby extends NewParent {      static int hoursOfCrying = 6 + (int) (Math.random() * 2.0);      static {         System.out.println(\"NewbornBaby was initialized.\");     } }  \/\/ On CD-ROM in file classlife\/ex2\/Example2.java class Example2 {      \/\/ Invoking main() is an active use of Example2     public static void main(String[] args) {          \/\/ Using hoursOfSleep is an active use of NewParent,         \/\/ but a passive use of NewbornBaby         int hours = NewbornBaby.hoursOfSleep;         System.out.println(hours);     }      static {         System.out.println(\"Example2 was initialized.\");     } } <\/code><\/pre>\n<\/blockquote>\n<p>\u7136\u540e\u5b83\u8bf4\uff1a<\/p>\n<blockquote>\n<p>In the above example, executing main() of Example2 causes only Example2 and NewParent to be initialized. NewbornBaby is not initialized and need not be loaded.<\/p>\n<\/blockquote>\n<p>\u6211\u660e\u767d\u201cNewbornBaby is not initialized\u201d\uff0c\u4f46\u662f\u4e3a\u4ec0\u4e48\u201cNewbornBaby need not be loaded\u201d\u5462\uff1f\u5982\u679c NewbornBaby \u90fd\u6ca1\u6709\u88ab loaded\uff0c\u90a3\u4e48 JVM \u600e\u4e48\u53ef\u80fd\u77e5\u9053 hoursOfSleep \u662f\u6765\u6e90\u4e8e NewParent \u7684\u5462\uff1f<\/p>\n<p><code>java -verbose:class Example2<\/code>\u7684\u8f93\u51fa\u7247\u6bb5\u5982\u4e0b\uff1a<\/p>\n<pre><code>[Loaded Example2 from file:\/Users\/jason\/trivial\/] [Loaded sun.launcher.LauncherHelper$FXHelper from \/Library\/Java\/JavaVirtualMachines\/jdk1.8.0_181.jdk\/Contents\/Home\/jre\/lib\/rt.jar] [Loaded java.lang.Class$MethodArray from \/Library\/Java\/JavaVirtualMachines\/jdk1.8.0_181.jdk\/Contents\/Home\/jre\/lib\/rt.jar] [Loaded java.lang.Void from \/Library\/Java\/JavaVirtualMachines\/jdk1.8.0_181.jdk\/Contents\/Home\/jre\/lib\/rt.jar] Example2 was initialized. [Loaded NewParent from file:\/Users\/jason\/trivial\/] [Loaded NewbornBaby from file:\/Users\/jason\/trivial\/] [Loaded java.lang.Math$RandomNumberGeneratorHolder from \/Library\/Java\/JavaVirtualMachines\/jdk1.8.0_181.jdk\/Contents\/Home\/jre\/lib\/rt.jar] [Loaded java.util.Random from \/Library\/Java\/JavaVirtualMachines\/jdk1.8.0_181.jdk\/Contents\/Home\/jre\/lib\/rt.jar] NewParent was initialized. 1 <\/code><\/pre>\n<p>\u867d\u7136\u7ed3\u679c\u663e\u793a NewbornBaby \u88ab loaded \u4e86\uff0c\u4f46\u662f\u4e3a\u4ec0\u4e48 NewParent \u5148\u4e8e NewbornBaby \u88ab loaded \u5462\uff1f<\/p>\n<\/p><\/div>\n<p><\/p>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>22<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3204482\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ik2h <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5c31\u7b97\u4ece\u4e0d\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b,\u7c7b\u5b57\u6bb5\u4e5f\u548c\u7c7b\u5173\u8054,\u7c7b\u5b57\u6bb5\u4f1a\u5728\u8c03\u7528\u6784\u9020\u65b9\u6cd5\u524d\u521d\u59cb\u5316,javac \u4f1a\u4e3a\u6bcf\u4e2a\u7c7b\u81ea\u52a8\u751f\u6210\u4e00\u4e2a\u7c7b\u521d\u59cb\u5316\u65b9\u6cd5,\u7c7b\u5b57\u6bb5\u4f1a\u5728\u8fd9\u4e2a\u65b9\u6cd5\u91cc\u521d\u59cb\u5316,\u7c7b\u7684\u521d\u59cb\u5316\u662f\u5185\u90e8\u65b9\u6cd5,\u5bf9\u7a0b\u5e8f\u5458\u4e0d\u53ef\u89c1. <\/p>\n<p>\u5982\u679c\u6211\u6ca1\u8bb0\u9519,\u53ef\u4ee5\u7528 javap \u770b\u770b\u521d\u59cb\u5316\u65b9\u6cd5, \u4f60\u7ed9\u7684\u8fd9\u4e2a\u4f8b\u5b50\u5168\u662f static,\u76f4\u63a5\u770b\u6df1\u5165 static \u65b9\u9762\u7684\u77e5\u8bc6\u5c31\u597d\u4e86                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3204483\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : JasonLaw <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ik2h #1 \u4f60\u8bf4\u7684\u90fd\u662f\u5173\u4e8e initialization \u7684\uff0c\u800c\u6211\u7684\u95ee\u9898\u662f\u5173\u4e8e loading \u7684\u3002<\/p>\n<p>\u987a\u4fbf\u8bf4\u4e00\u4e0b\uff0c\u201cjavac \u4f1a\u4e3a\u6bcf\u4e2a\u7c7b\u81ea\u52a8\u751f\u6210\u4e00\u4e2a\u7c7b\u521d\u59cb\u5316\u65b9\u6cd5\u201d\u4e0d\u662f\u5b8c\u5168\u6b63\u786e\u7684\u3002<\/p>\n<p>\u5173\u4e8e\u4ec0\u4e48\u60c5\u51b5\u4f1a\u4ea7\u751f()\u65b9\u6cd5\uff0chttps:\/\/www.artima.com\/insidejvm\/ed2\/lifetype4.html \u4e2d\u63cf\u8ff0\u4e86\u5f88\u6e05\u695a\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u7247\u6bb5\uff1a<\/p>\n<p>Not all classes will necessarily have a () method in their class file. If a class declares no class variables or static initializers, it won&#8217;t have a () method. If a class declares class variables, but doesn&#8217;t explicitly initialize them with class variable initializers or static initializers, it won&#8217;t have a () method. If a class contains only class variable initializers for static final variables, and those class variable initializers use compile-time constant expressions, that class won&#8217;t have a () method. Only those classes that actually require Java code to be executed to initialize class variables to proper initial values will have a class initialization method.<\/p>\n<p>Interfaces may also be awarded a () method in the class file. All fields declared in an interface are implicitly public, static, and final and must be initialized with a field initializer. If an interface has any field initializers that don&#8217;t resolve at compile-time to a constant, that interface will have a () method.                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3204484\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ik2h <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @JasonLaw https:\/\/docs.oracle.com\/javase\/specs\/jls\/se7\/html\/jls-12.html <\/p>\n<p>A reference to a static field (\u00a78.3.1.1) causes initialization of only the class or interface that actually declares it, even though it might be referred to through the name of a subclass, a subinterface, or a class that implements an interface.                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3204485\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Jooooooooo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             load \u5b50\u7c7b\u80af\u5b9a\u8981\u5148 load \u7236\u7c7b\u5427                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3204486\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : JasonLaw <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ik2h #3 \u5148\u629b\u5f00\u5176\u4ed6\u7684\uff0c\u5355\u7eaf\u8ba8\u8bba 3 \u6240\u5f15\u7528\u7684\u5185\u5bb9\u3002<\/p>\n<p>\u201cA reference to a static field (\u00a78.3.1.1) causes initialization of only the class or interface that actually declares it\u201d\u4e0d\u4e00\u5b9a\u6b63\u786e\u3002\u4e3b\u9898\u6b63\u6587\u4e2d\u7684\u5916\u94fe\uff08\u56e0\u4e3a\u63d0\u793a\u56de\u590d\u4e0d\u80fd\u5305\u542b\u5916\u94fe\uff0c\u6240\u4ee5\u53ea\u80fd\u8fd9\u4e48\u5f04\uff09\u8bf4\u4e86\u201cA use of a field that is both static and final, and initialized by a compile-time constant expression, is not an active use of the type that declares the field.\u201d\uff0c\u5176\u4e2d\u7684 Example3 \u4e5f\u6f14\u793a\u4e86\uff08\u867d\u7136 Example3 \u4e2d\u5f15\u7528\u4e86 Angry.greeting \u548c Dog.greeting\uff0c\u4f46\u662f Angry \u548c Dog \u90fd\u6ca1\u6709\u88ab\u521d\u59cb\u5316\uff09\u3002<\/p>\n<p>\u8fd8\u662f\u6211\u54ea\u91cc\u7406\u89e3\u9519\u4e86\uff1f                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3204487\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ik2h <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @JasonLaw \u7f16\u8bd1\u65f6\u5e38\u91cf                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3204488\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ik2h <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @JasonLaw \u8fd9\u65b9\u9762\u662f\u5173\u4e8e\u7c7b\u7684\u4e3b\u52a8\u5f15\u7528\u548c\u88ab\u52a8\u5f15\u7528, \u6211\u8bb0\u5f97 final static \u5728 java \u7f16\u7a0b\u601d\u60f3\u91cc\u9762\u4e5f\u6709\u63d0\u53ca\u8fd9\u65b9\u9762\u7684\u5185\u5bb9                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3204489\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : JasonLaw <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ik2h #6 \u662f\u7684\uff0c\u6240\u4ee5\u201cA reference to a static field (\u00a78.3.1.1) causes initialization of only the class or interface that actually declares it\u201d\u4e0d\u4e00\u5b9a\u6b63\u786e\u5440\u3002\u96be\u9053\u201ca field that is both static and final, and initialized by a compile-time constant expression\u201d\u4e0d\u662f\u4e00\u4e2a\u201cstatic field\u201d\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3204490\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : JasonLaw <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Jooooooooo #4 \u4f60\u53ef\u4ee5\u770b\u770b\u7b2c\u4e00\u6761\u9644\u8a00\u3002\u65e2\u7136\u5b50\u7c7b\u6ca1\u6709\u5148\u88ab loaded\uff0cJVM \u662f\u600e\u4e48\u77e5\u9053\u5b50\u7c7b\u7684\u7236\u7c7b\u662f\u4ec0\u4e48\u5462\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3204491\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ik2h <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @JasonLaw \u300a\u6df1\u5165\u7406\u89e3 Java \u865a\u62df\u673a\u300b 7.2 \u6709\u6bd4\u8f83\u8be6\u7ec6\u7684\u89e3\u91ca,\u7f16\u8bd1\u65f6\u5e38\u91cf\u5728\u7f16\u8bd1\u9636\u6bb5\u4f1a\u5b58\u5165\u8c03\u7528\u7c7b\u7684\u5e38\u91cf\u6c60\u4e2d\uff0c\u672c\u8d28\u4e0a\u5e76\u6ca1\u6709\u76f4\u63a5\u5f15\u7528\u5230\u5b9a\u4e49\u5e38\u91cf\u7684\u7c7b\uff0c\u56e0\u6b64\u4e0d\u4f1a\u89e6\u53d1\u5b9a\u4e49\u5e38\u91cf\u7684\u7c7b\u7684\u521d\u59cb\u5316\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3204492\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Jooooooooo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @JasonLaw \u53ef\u4ee5\u6253\u5f00 .class \u6587\u4ef6\u770b\u4e00\u4e0b main \u65b9\u6cd5\u7684\u5b57\u8282\u7801\u662f\u600e\u4e48\u6837\u7684. \u7279\u522b\u662f\u8fd9\u4e00\u53e5 int hours = NewbornBaby.hoursOfSleep; \u5f15\u7528\u7684\u5e38\u91cf\u6c60\u662f\u600e\u4e48\u5bf9\u5e94\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3204493\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : JasonLaw <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Jooooooooo #11 &#8220;int hours = NewbornBaby.hoursOfSleep;&#8221;\u5bf9\u5e94\u7684\u5b57\u8282\u7801\u4e3a&#8221;getstatic #2 \u548c istore_1&#8243;\u3002\u5728 constant pool \u4e2d\uff0cindex \u4e3a 2 \u7684 entry \u662f\u4e00\u4e2a CONSTANT_Fieldref_info\uff0c\u901a\u8fc7 CONSTANT_Fieldref_info \u4e2d\u7684 class_index \u6700\u540e\u4f1a\u5f97\u5230 NewbornBaby\uff0c\u901a\u8fc7 CONSTANT_Fieldref_info \u4e2d\u7684 name_and_type_index\uff0c\u6700\u540e\u4f1a\u5f97\u5230\u4e00\u4e2a CONSTANT_NameAndType_info\uff0c\u901a\u8fc7 CONSTANT_NameAndType_info \u4e2d\u7684 name_index \u6700\u540e\u4f1a\u5f97\u5230 hoursOfSleep\uff0c\u901a\u8fc7 CONSTANT_NameAndType_info \u4e2d\u7684 descriptor_index \u6700\u540e\u4f1a\u5f97\u5230 I \u3002<\/p>\n<p>\u7136\u540e\u5462\uff1f\u4e3a\u4ec0\u4e48\u7236\u7c7b\u5148\u4e8e\u5b50\u7c7b\u88ab loaded \uff1f\u5b50\u7c7b\u6ca1\u6709\u5148\u88ab loaded\uff0cJVM \u662f\u600e\u4e48\u77e5\u9053\u5b50\u7c7b\u7684\u7236\u7c7b\u662f\u4ec0\u4e48\u5462\uff1f                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3204494\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : JasonLaw <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ik2h #10 \u6211\u611f\u89c9\uff0c\u6211\u4eec\u6839\u672c\u4e0d\u662f\u5728\u8ba8\u8bba\u4e00\u4e2a\u4e1c\u897f\uff0c\u76f8\u5173\u7684\u56de\u590d\u4e5f\u6ca1\u6709\u4ec0\u4e48\u4e0a\u4e0b\u6587\u5173\u7cfb\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3204495\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : mind3x <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @JasonLaw <br \/>&gt; \u201cA reference to a static field (\u00a78.3.1.1) causes initialization of only the class or interface that actually declares it\u201d\u4e0d\u4e00\u5b9a\u6b63\u786e\u3002<\/p>\n<p>@ik2h \u5728 #3 \u4e2d\u5f15\u7528\u7684\u662f JLS\uff0c\u867d\u7136\u7248\u672c\u65e7\u4e86\u70b9\uff0c\u597d\u6b79\u4e5f\u662f\u6b63\u5b97\u5b98\u65b9\u7684\u8bed\u8a00 specification\uff0c\u4f55\u6765\u7684\u201c\u4e0d\u4e00\u5b9a\u6b63\u786e\u201d\uff1f\u4f60\u5173\u5fc3\u7684\u95ee\u9898\u672c\u6765\u5c31\u5e94\u8be5\u4ece JLS \u548c JVM Spec \u91cc\u9762\u5bfb\u627e\u7b54\u6848\u3002<\/p>\n<p>\u81f3\u4e8e\u4f60\u7684\u6838\u5fc3\u95ee\u9898 NewParent \u4e3a\u4ec0\u4e48\u5148\u4e8e NewbornBaby \u88ab load\uff0c\u539f\u56e0\u5f88\u7b80\u5355\uff1a\u7c7b\u52a0\u8f7d\u662f\u4e00\u8fb9\u89e3\u6790\u4e00\u8fb9\u9012\u5f52\u7684\u2014\u2014\u8fd9\u91cc\u7684\u987a\u5e8f\u662f \u5f00\u59cb\u52a0\u8f7d NewbornBaby -&gt; \u89e3\u6790 NewbornBaby -&gt; \u53d1\u73b0\u7236\u7c7b NewParent -&gt; (\u9012\u5f52) \u5f00\u59cb\u52a0\u8f7d NewParent -&gt; &#8230; -&gt; \u52a0\u8f7d NewParent \u7ed3\u675f -&gt; \u7ee7\u7eed\u52a0\u8f7d NewbornBaby -&gt; \u52a0\u8f7d NewParent \u7ed3\u675f\u3002\u4f60\u770b\u5230\u7684 log \u53ea\u662f\u6253\u4e86 load \u7ed3\u675f\u800c\u5df2\u3002<\/p>\n<p>JVM Spec (1.7 \u7248) 5.3: https:\/\/docs.oracle.com\/javase\/specs\/jvms\/se7\/html\/jvms-5.html#jvms-5.3<\/p>\n<p>&gt; A subtlety here is that recursive class loading to load superclasses is performed as part of resolution (\u00a75.3.5, step 3). Therefore, a ClassNotFoundException that results from a class loader failing to load a superclass must be wrapped in a NoClassDefFoundError.                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3204496\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : mind3x <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @mind3x #14<\/p>\n<p>typo: &#8220;\u7ee7\u7eed\u52a0\u8f7d NewbornBaby -&gt; \u52a0\u8f7d NewParent \u7ed3\u675f&#8221; \u5e94\u4e3a &#8220;\u7ee7\u7eed\u52a0\u8f7d NewbornBaby -&gt; \u52a0\u8f7d NewbornBaby&#8221; \u7ed3\u675f\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3204497\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : JasonLaw <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Jooooooooo #4 \u5b50\u7c7b\u8fd8\u662f\u8981\u5148\u88ab loaded \u7684\uff0c\u53ea\u662f\u7236\u7c7b\u5148\u88ab loaded \u5b8c\u6210\uff0c\u5177\u4f53\u53ef\u4ee5\u770b\u770b\u7b2c 2 \u6761\u9644\u8a00\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3204498\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : JasonLaw <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @mind3x #14 <br \/>@mind3x #15 <\/p>\n<p>\u201c\u4e3a\u4ec0\u4e48 NewParent \u5148\u4e8e NewbornBaby \u88ab loaded\u201d\u662f\u6211\u81ea\u5df1\u6ca1\u6709\u8ba4\u771f\u770b\u6e05\u695a\u7684\u95ee\u9898\uff0c\u6211\u7684\u6838\u5fc3\u95ee\u9898\u5e76\u4e0d\u662f\u8fd9\u4e2a\uff0c\u6211\u7684\u6838\u5fc3\u95ee\u9898\u662f\u201c\u4e3a\u4ec0\u4e48 NewbornBaby need not be loaded\u201d\u3002\u5173\u4e8e\u8fd9\u4e2a\u95ee\u9898\uff0cHolger \u5728 Stack Overflow \u4e0a\u9762\u56de\u7b54\u6211\u4e86\uff0c\u5177\u4f53\u53ef\u4ee5\u770b\u770b\u7b2c 2 \u6761\u9644\u8a00\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3204499\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : JasonLaw <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @mind3x #14 \u4f60\u8bf4\u201cA reference to a static field (\u00a78.3.1.1) causes initialization of only the class or interface that actually declares it\u201d\u6765\u6e90\u4e8e\u5b98\u65b9\u6587\u6863\uff0c\u4e00\u5b9a\u662f\u6b63\u786e\u7684\u3002\u96be\u9053\u201ca field that is both static and final, and initialized by a compile-time constant expression\u201d\u4e0d\u662f\u4e00\u4e2a\u201cstatic field\u201d\uff1f<\/p>\n<p>\u6bd4\u5982\u6211\u5c06 NewbornBaby \u6539\u4e3a\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<p>&#8220;`<br \/>class NewbornBaby extends NewParent {<\/p>\n<p> static int hoursOfCrying = 6 + (int) (Math.random() * 2.0);<\/p>\n<p> static final int hoursOfSleep = 6; \/\/ a field that is both static and final, and initialized by a compile-time constant expression<\/p>\n<p> static {<br \/> System.out.println(&#8220;NewbornBaby was initialized.&#8221;);<br \/> }<br \/>}<br \/>&#8220;`<\/p>\n<p>\u8fd0\u884c java -verbose Example2\uff0c\u53ef\u4ee5\u770b\u51fa NewbornBaby \u88ab\u6ca1\u6709\u88ab loaded \u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3204500\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Jooooooooo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @JasonLaw \u4f60\u6700\u65e9\u770b\u7684\u90a3\u7bc7\u662f\u6587\u7ae0\u662f\u9519\u7684\u86ee\u5751\u7684.                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3204501\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : JasonLaw <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Jooooooooo #19 \u5176\u5b9e Inside the Java Virtual Machine \u8fd8\u662f\u5f88\u597d\u7684\uff0c\u73b0\u5728\u770b\u4e86 Chapter 5-7\uff0c\u53ea\u53d1\u73b0\u4e86\u8fd9\u4e00\u4e2a\u9519\u8bef\uff0c\u5176\u4ed6\u7684\u90fd\u89e3\u91ca\u5f97\u5f88\u597d\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3204502\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : mind3x <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @JasonLaw <\/p>\n<p>#17 &#8220;NewbornBaby need not be loaded&#8221; \u8fd9\u53e5\u8bdd\u662f\u9519\u7684\uff0c\u5f88\u591a\u4e66\u90fd\u6709\u9519\uff0c\u8fd9\u4e2a\u6b63\u5e38\u3002\u4ee5 JLS, JVMS \u4e3a\u51c6\u3002<\/p>\n<p>#18 \u6ca1\u9519\uff0c&#8221;A reference to a static field (\u00a78.3.1.1) causes initialization of only the class or interface that actually declares it&#8221; \u662f\u6b63\u786e\u7684\u3002\u8fd9\u91cc\u7684\u91cd\u70b9\u662f &#8220;reference&#8221; (\u6572\u9ed1\u677f)\u3002\u4f60\u52a0\u4e86 final\uff0c\u8fd9\u4e2a\u503c\u5c31\u662f\u4e2a\u5e38\u91cf\uff0c\u76f4\u63a5\u5728\u7f16\u8bd1\u65f6\u653e\u8fdb Example2 \u7684\u5e38\u91cf\u6c60\u4e86\uff0c\u800c\u4e0d\u662f\u5f15\u7528 NewbornBaby \u3002\u4f60\u53ef\u4ee5\u770b\u5b57\u8282\u7801\u7684\u533a\u522b\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3204503\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : JasonLaw <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @mind3x \u5982\u679c\u5bf9\u201ca reference to a static field\u201d\u662f\u8fd9\u6837\u7406\u89e3\u7684\u8bdd\uff0c\u90a3\u53e5\u8bdd\u7684\u786e\u662f\u5bf9\u7684\u3002\u6211\u7684\u5173\u6ce8\u70b9\u662f source code\uff0c\u800c\u4f60\u7684\u662f compiled code \u3002\u603b\u4e4b\u8c22\u8c22\u4f60\u7684\u56de\u590d\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u4e8e\u201cInside the Jav&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\/152867"}],"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=152867"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/152867\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=152867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=152867"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=152867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}