“加载类时”是什么意思?实际上是什么意思?

发布于 12-21 13:23 字数 192 浏览 5 评论 0原文

据说java中的静态块仅在加载该类时运行一次。但这实际上意味着什么呢?类在什么时候被 JVM(Java 虚拟机)加载?

是在调用该类中的 main 方法时吗?而且是不是在main方法开始执行的时候,同一个类的所有超类也都被加载了呢?

考虑 A 扩展 B 和 B 扩展 C。它们都有静态块。如果A有main方法,那么静态块的执行顺序是什么?

It is said that static blocks in java run only once when that class is loaded. But what does it actually mean? At which point is a class loaded by JVM (Java Virtual Machine)?

Is it when the main method in that class is called? And is it that all the super-classes of the same class are also loaded when the main method starts execution?

Consider that A extends B and B extends C. All have static blocks. If A has the main method, then what will be the sequence of execution of static blocks?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小ぇ时光︴2024-12-28 13:23:33

这在执行部分中进行了描述JLS。即:

类的初始化包括执行其静态初始化程序和类中声明的静态字段的初始化程序。接口的初始化包括执行接口中声明的字段的初始化程序。
在初始化类之前,必须初始化其直接超类,但该类实现的接口不需要初始化。同样,接口的超级接口不需要在接口初始化之前初始化。

因此,在您的示例中,“最顶层”类 (C) 的静态块首先运行,然后是 B 的静态块,然后是最派生的类。

有关加载类的所有步骤的详细说明,请参阅该文档。

(类在第一次被使用时被加载。)

This is described in the Execution section of the JLS. Namely:

Initialization of a class consists of executing its static initializers and the initializers for static fields declared in the class. Initialization of an interface consists of executing the initializers for fields declared in the interface.
Before a class is initialized, its direct superclass must be initialized, but interfaces implemented by the class need not be initialized. Similarly, the superinterfaces of an interface need not be initialized before the interface is initialized.

So in your example, the static block of the "topmost" class (C) runs first, then that of B, then the most-derived one.

See that documentation for a detailed description of all the steps that go into loading a class.

(Classes get loaded when they are first actively used.)

如歌彻婉言2024-12-28 13:23:33

我认为下面的示例将解决您的所有问题:

在初始化类之前,如果先前尚未初始化其超类,则对其超类进行初始化。

因此,测试程序:

class Super {
        static { System.out.print("Super "); }
}
class One {
        static { System.out.print("One "); }
}
class Two extends Super {
        static { System.out.print("Two "); }
}
class Test {
        public static void main(String[] args) {
                One o = null;
                Two t = new Two();
                System.out.println((Object)o == (Object)t);
        }
}

打印:

Super Two false

类 One 从未被初始化,因为它没有被主动使用,因此从未被链接到。类 Two 仅在其超类 Super 初始化后才初始化。

有关更多详细信息,请访问此链接

编辑详细信息:删除令人困惑的内容线。

I think the following example will solve all of your problems:

Before a class is initialized, its superclasses are initialized, if they have not previously been initialized.

Thus, the test program:

class Super {
        static { System.out.print("Super "); }
}
class One {
        static { System.out.print("One "); }
}
class Two extends Super {
        static { System.out.print("Two "); }
}
class Test {
        public static void main(String[] args) {
                One o = null;
                Two t = new Two();
                System.out.println((Object)o == (Object)t);
        }
}

prints:

Super Two false

The class One is never initialized, because it not used actively and therefore is never linked to. The class Two is initialized only after its superclass Super has been initialized.

For more details visit this link

Edit details: Removed confusing lines.

居里长安2024-12-28 13:23:33

来自 Java 语言规范

类的初始化包括执行其静态初始化程序和类中声明的静态字段(类变量)的初始化程序。接口的初始化包括执行其中声明的字段(常量)的初始值设定项。

在类初始化之前,必须初始化其超类,但该类实现的接口不会被初始化。同样,在接口初始化之前,接口的超级接口也不会被初始化。

Java 中更详细地描述了该过程虚拟机规范

From the Java Language Specification:

Initialization of a class consists of executing its static initializers and the initializers for static fields (class variables) declared in the class. Initialization of an interface consists of executing the initializers for fields (constants) declared there.

Before a class is initialized, its superclass must be initialized, but interfaces implemented by the class are not initialized. Similarly, the superinterfaces of an interface are not initialized before the interface is initialized.

The process is described in more detail in the Java Virtual Machine Specification.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文