何时加载类? [英] When classes are loaded?

查看:41
本文介绍了何时加载类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,我有这样的代码:

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:

  1. Test t; -它不是活动用法,但是引用 t 必须是它的确定类型.是加载了Test类(也许是部分加载,然后通过了几个阶段-loading \ linking \ initialization-它通过了)还是什么都没有发生?
  2. Integer i = new Integer(1); -在JVM启动时或在此行时是否已加载Integer?
  3. t = new Test(); -活跃用法.它是从一开始还是从某个点完全加载(请参阅1)
  4. System.out.println(Test4.a); -是否已加载 Test4 ?
  5. 是否已加载 Test2 Test3 ?如果是,那么什么时候?
  1. Test t; - it is not an active usage, but the reference t must be a definite type of. Was Test class loaded (maybe partially, then how many stages - loading\linking\initializing - it passed) or nothing happened?
  2. Integer i = new Integer(1); - was Integer loaded when JVM starts or on this line?
  3. t = new Test(); - an active usage. Was it loaded fully from the beginning or from some point (see 1)
  4. System.out.println(Test4.a); - was Test4 loaded or not?
  5. Were Test2 and Test3 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).

您的问题:

  1. 声明变量不会加载该类.但是加载发生在链接之前,而链接发生在初始化之前.因此,在加载类时,也将其链接并初始化.
  2. Integer 类在代码运行之前由JVM(以及许多其他语言基础类)加载.
  3. 现在已加载 Test 类,因为已创建了新实例.
  4. 未加载
  5. Test4 ,因为仅使用了一个常量变量,与上述第4条规则冲突.
  6. 加载 Test 后,会加载
  7. Test3 Test2 ,因为 Test2 对象是在中创建的测试的实例初始值设定项,这也会导致加载 Test3 (超类).
  1. Declaring a variable doesn't load the class. But loading happens before linking, and linking happens before initialization. So, when a class is loaded, it is also then linked and initialized.
  2. The Integer class is loaded by the JVM (along with lots of other language foundation classes) before your code runs.
  3. Now the Test class is loaded, because a new instance is created.
  4. Test4 was not loaded, because only a constant variable was used, which conflicts with the 4th rule above.
  5. 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.

使用JVM选项 -verbose:class .

这篇关于何时加载类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆