简体   繁体   English

关于类加载的困惑

[英]Confusion about class loading

I am a little bit confused about when the class actually is loaded by the JVM. 当JVM实际加载类时,我有点困惑。 I noticed that the class loader will load the class when the class is referenced. 我注意到类加载器将在引用类时加载类。

I am using java6 environment and run with -verbose:class for tracking class loading. 我正在使用java6环境并使用-verbose:class运行以跟踪类加载。 For example: 例如:

MyObject obj = new MyObject(); //MyObject.class will be loaded

However, in this case 但是,在这种情况下

// ClassC.java
package com.gogog22510.test;
public class ClassC {}

// ClassB.java
package com.gogog22510.test;
public class ClassB extends ClassC {}

// ClassA.java
package com.gogog22510.test;
public class ClassA {
    public ClassC test() {
        return new ClassB();
    }
}

when my test program initialize ClassA, it will load all ClassA, ClassB, and ClassC even though I haven't invoked test() 当我的测试程序初始化ClassA时,即使我没有调用test(),它也会加载所有ClassA,ClassB和ClassC

// TestClassLoad.java
package com.gogog22510.test;
public class TestClassLoad {
    public static void main(String[] args) {
        // initialize ClassA
        System.out.println("start load ClassA");
        new ClassA();
    }
}

console: 安慰:

[Loaded TestClassLoad from file:/.../bin/] [从文件中加载TestClassLoad:/.../ bin /]
start load ClassA 开始加载ClassA
[Loaded ClassA from file:/C:/.../bin/] [从文件中加载ClassA:/ C:/.../ bin /]
[Loaded ClassC from file:/C:/.../bin/] [从文件中加载ClassC:/ C:/.../ bin /]
[Loaded ClassB from file:/C:/.../bin/] [从文件中加载ClassB:/ C:/.../ bin /]

But if I change the test() method's return type like this: 但是,如果我改变test()方法的返回类型,如下所示:

// ClassA.java
package com.gogog22510.test;
public class ClassA {
    public ClassB test() {
        return new ClassB();
    }
}

ClassLoader will only load ClassA into perm space unless I invoke the test() method. 除非我调用test()方法,否则ClassLoader只会将ClassA加载到perm空间。

console: 安慰:

[Loaded TestClassLoad from file:/.../bin/] [从文件中加载TestClassLoad:/.../ bin /]
start load ClassA 开始加载ClassA
[Loaded ClassA from file:/C:/.../bin/] [从文件中加载ClassA:/ C:/.../ bin /]

Why does the ClassLoader load all three class before I explicitly call the method? 为什么ClassLoader在显式调用方法之前加载所有三个类?

I think it happens during the verification step, to make sure that ClassC is a subclass of ClassB , for purposes of validating the return type. 我认为它发生在验证步骤中,以确保ClassCClassB的子类,以验证返回类型。

Here , they say: 在这里 ,他们说:

If the method returns a reference type, it must do so using an areturn instruction, and the type of the returned value must be assignment compatible (JLS §5.2) with the return descriptor (§4.3.3) of the method. 如果方法返回引用类型,则必须使用areturn指令执行此操作,并且返回值的类型必须与方法的返回描述符(第4.3.3节)分配兼容(JLS§5.2)。

See also this : 另见

Verification (§4.10) ensures that the binary representation of a class or interface is structurally correct (§4.9). 验证(第4.10节)确保类或接口的二进制表示在结构上是正确的(第4.9节)。 Verification may cause additional classes and interfaces to be loaded (§5.3) but need not cause them to be verified or prepared. 验证可能会导致加载其他类和接口(第5.3节),但不需要对它们进行验证或准备。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM