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

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 呢?

Java-向Servlet动态添加URL模式 - java

是否可以在运行时动态地将URL模式添加到Servlet?例如,当Servlet启动时,扫描文件夹中的注释,然后将这些URL模式注入到Servlet中?提供更多清晰度-在Servlet的init文件中,我要执行此操作(伪代码)// scan all the files in the package my.project.services // find all…

执行shell命令时永久挂起(Java) - java

令人讨厌的代码块如下。该代码几乎总是可以工作,但有时会永远挂起。该应用程序是一个EJB计时器bean。实际上,它只挂了一次,我无法复制它。它的工作已经有将近两年没有任何问题。但是,在测试应用程序的更新版本时,计时器仅在运行几天后冻结,而从未从上次运行时释放数据库锁。日志清楚地表明它冻结在下面的代码块中的某个位置。它正在运行的命令是“ chmod”。publi…

如何检索字符串的特定部分 - java

我有一个目录以字符串形式列出,我想检索字符串的特定部分,唯一的是,由于这是一个目录,它的长度可以更改我想从字符串中检索文件名"C:\projects\Compiler\Compiler\src\JUnit\ExampleTest.java" "C:\projects\ExampleTest.java" 因此,在这两种情…

Java:增加YoungGen大小以提高GC性能 - java

我正在阅读以下文章:http://java.sun.com/docs/hotspot/gc1.4.2/example.html,无法理解以下几行:Young generation size is too small The young generation heap size in this first example is about 4 Mbytes w…

如果仅对BinaryOperator参数之一进行汇总,那么Java流实际上会减少什么? - java

看一下下面的代码:在二进制运算符中,我们有reduce((x,y)-> x + x)。为什么实际上计算为Optional [512]?我没有解释System.out.println((Stream.generate(()->1d).limit(10). peek((doubleValue)->{ System.out.println(�…