何时加载类? [英] When classes are loaded?
问题描述
嗯,我有这样的代码:
public class Main {
public static void main(String[] args) {
Test t; //1
Integer i = new Integer(1); //2
t = new Test(); //3
System.out.println(Test4.a); //4
}
}
class Test {
private int a = 10;
private Test2 t2; //5
List<Test2> list = new ArrayList<Test2>() {
{
for (int i = 0; i < a; i++) {
add(new Test2()); //6
}
}
};
}
class Test2 extends Test3{
}
class Test3 {
}
class Test4 {
public static final int a = 4;
}
我不知道如何(全部或部分)以及何时加载类.所以:
I don't know how (fully or partially) and when classes are loaded. So:
-
Test t;
-它不是活动用法,但是引用t
必须是它的确定类型.是加载了Test类(也许是部分加载,然后通过了几个阶段-loading \ linking \ initialization-它通过了)还是什么都没有发生? -
Integer i = new Integer(1);
-在JVM启动时或在此行时是否已加载Integer? -
t = new Test();
-活跃用法.它是从一开始还是从某个点完全加载(请参阅1) -
System.out.println(Test4.a);
-是否已加载Test4
? - 是否已加载
Test2
和Test3
?如果是,那么什么时候?
Test t;
- it is not an active usage, but the referencet
must be a definite type of. Was Test class loaded (maybe partially, then how many stages - loading\linking\initializing - it passed) or nothing happened?Integer i = new Integer(1);
- was Integer loaded when JVM starts or on this line?t = new Test();
- an active usage. Was it loaded fully from the beginning or from some point (see 1)System.out.println(Test4.a);
- wasTest4
loaded or not?- Were
Test2
andTest3
loaded or not? If yes then when?
推荐答案
T的类或接口类型T会在以下任何一种首次出现之前立即初始化:
A class or interface type T will be initialized immediately before the first occurrence of any one of the following: T是一个类,并创建T的实例. T is a class and an instance of T is created. T是一个类,并调用T声明的静态方法. T is a class and a static method declared by T is invoked. 分配了由T声明的静态字段. A static field declared by T is assigned. 使用了T声明的静态字段,该字段不是常量变量(第4.12.4节). A static field declared by T is used and the field is not a constant variable (§4.12.4). T是顶级类(第7.6节),并执行一个词法嵌套在T(第8.1.3节)中的assert语句(第14.10节). T is a top level class (§7.6), and an assert statement (§14.10) lexically nested within T (§8.1.3) is executed. (剪裁) 在任何其他情况下,都不会初始化类或接口. A class or interface will not be initialized under any other circumstance. 第5章讨论了如何加载,链接和初始化类. Chapter 5 talks about loading, linking, and initializing classes. Java虚拟机动态加载,链接和初始化类和接口.加载是查找具有特定名称的类或接口类型的二进制表示形式并从该二进制表示形式创建类或接口的过程.链接是获取类或接口并将其组合到Java虚拟机的运行时状态以便可以执行的过程.类或接口的初始化包括执行类或接口的初始化方法(第2.9节). The Java Virtual Machine dynamically loads, links and initializes classes and interfaces. Loading is the process of finding the binary representation of a class or interface type with a particular name and creating a class or interface from that binary representation. Linking is the process of taking a class or interface and combining it into the run-time state of the Java Virtual Machine so that it can be executed. Initialization of a class or interface consists of executing the class or interface initialization method (§2.9). 您的问题: 这篇关于何时加载类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
Integer
类在代码运行之前由JVM(以及许多其他语言基础类)加载. Test
类,因为已创建了新实例. Test4
,因为仅使用了一个常量变量,与上述第4条规则冲突. Test
后,会加载 Test3
和 Test2
,因为 Test2
对象是在中创建的测试
的实例初始值设定项,这也会导致加载 Test3
(超类).
Integer
class is loaded by the JVM (along with lots of other language foundation classes) before your code runs.Test
class is loaded, because a new instance is created.Test4
was not loaded, because only a constant variable was used, which conflicts with the 4th rule above.Test3
and Test2
were loaded after Test
was loaded, because Test2
objects were created in Test
's instance initializer, which also caused Test3
(the superclass) to be loaded.