有人能解释一下Class.forName()和Thread.currentThread().getContextClassLoader().loadClass()之间的区别吗?
我有下面的代码:
<code>
public class Test {
static {
System.out.println("Hello from static block");
}
}
public class Application {
public static void main(String[] args) throws ClassNotFoundException {
//Class test = Class.forName("com.home.Test"); //Outputs "Hello from static block"
Thread.currentThread().getContextClassLoader().loadClass("com.home.Test");
}
}
</code>
当我执行 Class.forName("com.home.Test") 时,它会输出 "Hello from static block"。但是当我执行 Thread.currentThread().getContextClassLoader().loadClass("com.home.Test"); 时,它没有输出任何内容。