简体   繁体   中英

Why the Java ClassLoader is loading this class

I'm trying to understand why the JVM decides to load certain classes when it doesn't appear it needs to. Consider the following example code:

public class Foo {
    public void foo() {
    }

    static class Bar {
    }

    public Bar bar() {
        return new Bar();
    }
}

When calling new Foo().foo() , Bar is not loaded by the classloader since it's not needed. But, if we change the example to have bar return an instance of a sub-class:

public class Foo {    
    public void foo() {
    }

    static class Bar {
    }

    static class BigBar extends Bar {
    }

    public Bar bar() {
        return new BigBar();
    }
}

Calling new Foo().foo() causes the classloader to load both the Bar and BigBar classes even though neither of them are needed. Why?

Aside from this specific scenario, is there a way to learn why the JVM decides it needs to load a class, in general?

Here is a good read from Internals of Java Class Loading

Whenever a new JVM is started by typing java MyMainClass, the "bootstrap class loader" is responsible for loading key Java classes like java.lang.Object and other runtime code into memory first. The runtime classes are packaged inside of the JRE\\lib\\rt.jar file. We cannot find the details of the bootstrap class loader in the Java documentation, since this is a native implementation. For the same reason, the behavior of the bootstrap class loader will also differ across JVMs.

In a related note, we will get null if we try to get the class loader of a core Java runtime class, like this:

 log(java.lang.String.class.getClassLoader()); 

Next comes the Java extension class loader. We can store extension libraries, those that provide features that go beyond the core Java runtime code, in the path given by the java.ext.dirs property. The ExtClassLoader is responsible for loading all .jar files kept in the java.ext.dirs path. A developer can add his or her own application .jar files or whatever libraries he or she might need to add to the classpath to this extension directory so that they will be loaded by the extension class loader.

The third and most important class loader from the developer perspective is the AppClassLoader. The application class loader is responsible for loading all of the classes kept in the path corresponding to the java.class.path system property.

well explained http://javarevisited.blogspot.in/2012/07/when-class-loading-initialization-java-example.html

When Class is loaded in Java Class loading is done by ClassLoaders in Java which can be implemented to eagerly load a class as soon as another class references it or lazy load the class until a need of class initialization occurs. If Class is loaded before its actually being used it can sit inside before being initialized. I believe this may vary from JVM to JVM. While its guaranteed by JLS that a class will be loaded when there is a need of static initialization. When a Class is initialized in Java 1) an Instance of class is created using either new() keyword or using reflection using class.forName(), which may throw ClassNotFoundException in Java.

2) an static method of Class is invoked. 3) an static field of Class is assigned. 4) an static field of class is used which is not a constant variable. 5) if Class is a top level class and an assert statement lexically nested within class is executed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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