简体   繁体   English

Java如何找到已经加载的类?

[英]How does Java find already loaded classes?

I know that Java uses the ClassLoader hierarchy for loading the classes. 我知道Java使用ClassLoader层次结构来加载类。

For example a program: 例如一个程序:

public void test(){
    A a = new A(); // Line 1 The class is accessed first time here so it should be loaded and defined

    A ab = new A(); //Line 2 How can the second line be represented?
}

The first line of the code is similar to 代码的第一行类似于

Thread.currentThread().getContextClassLoader().loadClass("A");

So the class is loaded and defined to create instance of Class . 因此,将加载并定义该类以创建Class实例。

Now the question is when the second line is executed the Class A is referred again, will Java not lookup for the class again and return the same loaded instance of the Class? 现在的问题是,当第二行执行时,再次引用了Class A ,Java不会再次查找该类并返回相同的Class加载实例吗?

As the Java classloader document says that every class loader should maintain the instances of loaded classes and return the same instances for the next call. 正如Java类加载器文档所述,每个类加载器都应维护已加载类的实例,并为下一次调用返回相同的实例。

Where does Java keep the loaded classes? Java在哪里保存加载的类? ClassLoader class has a Vector of classes which is called by VM to add the loaded classes. ClassLoader类具有一个Vector类,VM会调用该类来添加已加载的类。

Maybe the question is a bit confusing, basically I am trying to figure out from which method are the already loaded classes returned. 也许这个问题有点令人困惑,基本上我正在尝试找出从哪个方法返回已经加载的类。 I tried to keep a debug point in the loadClass() method but it is not called for the Line 2 . 我尝试将调试点保留在loadClass()方法中,但Line 2未调用它。

The loadClass() method of ClassLoader has findLoadedClass method but that too is not called. ClassLoaderloadClass()方法具有findLoadedClass方法,但也不会被调用。

If you want to "translate" the mention of A to any method call, then the closest you could get is not loadClass() but Class.forName() . 如果您想将对A的提及“翻译”为任何方法调用,那么最接近的不是loadClass()而是Class.forName()

This method call queries the classloader for the class, which may or may not trigger class loading (and the caller doesn't even care). 该方法调用向类加载器查询类,这可能触发也可能不会触发类加载(并且调用者甚至不在乎)。 It will simply return a fully loaded (and initialized, if you don't use the three-argument version ) class back to the caller. 它将简单地将一个完全加载的(并初始化,如果您不使用三参数版本的 )类返回给调用者。

And once the class has been loaded, the class loader no longer get's invoked when the class is used (as the name suggests, it's job is done, once it loaded the class). 并且一旦加载了类,使用该类时就不再调用该类加载器(顾名思义,一旦加载了该类,就完成了工作)。

package java_language;

public class NewClass {

    Java_language j;

    public NewClass() throws ClassNotFoundException {
        j=new Java_language();
         if (Class.forName("java_language.Java_language", true, Thread.currentThread().getContextClassLoader()).equals(j.getClass())) {
            System.out.println("CLass has been loaded");
        }
    }

    public static void main(String[] args) throws ClassNotFoundException {
       new NewClass();

    }

}

package java_language;

public class Java_language {

    static Java_language java_language = null;
    public Java_language() {
        System.out.println("Stack Overflow");
    }
}

Ans is: Ans是:
回答截图

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

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