关于“Inside the Java Virtual Machine - Chapter 7 The Lifetime of a Type - Initialization”的疑问

2020-09-05 11:22:03 +08:00
 JasonLaw

Inside the Java Virtual Machine - Chapter 7 The Lifetime of a Type - Initialization中,有这么一段:

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--uses that won't trigger their initialization. They are an active use only of the class or interface in which the field is actually declared. Here's an example that illustrates this principle:

// 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.");
    }
}

然后它说:

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.


我明白“NewbornBaby is not initialized”,但是为什么“NewbornBaby need not be loaded”呢?如果 NewbornBaby 都没有被 loaded,那么 JVM 怎么可能知道 hoursOfSleep 是来源于 NewParent 的呢?

java -verbose:class Example2的输出片段如下:

[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

虽然结果显示 NewbornBaby 被 loaded 了,但是为什么 NewParent 先于 NewbornBaby 被 loaded 呢?

1854 次点击
所在节点    Java
22 条回复
mind3x
2020-09-07 15:10:14 +08:00
@JasonLaw

#17 "NewbornBaby need not be loaded" 这句话是错的,很多书都有错,这个正常。以 JLS, JVMS 为准。

#18 没错,"A reference to a static field (§8.3.1.1) causes initialization of only the class or interface that actually declares it" 是正确的。这里的重点是 "reference" (敲黑板)。你加了 final,这个值就是个常量,直接在编译时放进 Example2 的常量池了,而不是引用 NewbornBaby 。你可以看字节码的区别。
JasonLaw
2020-09-07 18:56:19 +08:00
@mind3x 如果对“a reference to a static field”是这样理解的话,那句话的确是对的。我的关注点是 source code,而你的是 compiled code 。总之谢谢你的回复。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/704416

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX