丶创建类的实例
- new
- 反射
- clone
- 反序列化
丶操作类中的静态变量(包括访问、赋值等)
丶主动调用类的静态方法
丶调用Class.forName("包类名");
丶完成子类的初始化,也会完成对本类的初始化
丶初始化子类需要先初始化父类(父类未被初始化的情况)
丶该类是程序引导入口
丶main/test入口
注意~~
classloader.loadclass()不会触发类的初始化的哦,虽然他看起来很像做了些什么==
下图可以看出传入的resolve参数为false,这个参数是判断这个类是否会真正进入初始化步骤
loadClass()函数中:
public Class loadClass(String name) throws ClassNotFoundException {
Class loaded = this.findLoadedClass(name);
if (loaded != null && loaded.getClassLoader() == this) {
return loaded;
} else if (!this.filter.accept(name)) {
return super.loadClass(name);
} else {
ClassReader r;
try {
InputStream is = this.classPath.getResourceAsStream(name.replace('.', '/') + ".class");
if (is == null) {
throw new ClassNotFoundException(name);
}
try {
r = new ClassReader(is);
} finally {
is.close();
}
} catch (IOException var16) {
throw new ClassNotFoundException(name + ":" + var16.getMessage());
}
try {
DebuggingClassWriter w = new DebuggingClassWriter(2);
this.getGenerator(r).generateClass(w);
byte[] b = w.toByteArray();
Class c = super.defineClass(name, b, 0, b.length, DOMAIN);
this.postProcess(c);
return c;
} catch (RuntimeException var12) {
throw var12;
} catch (Error var13) {
throw var13;
} catch (Exception var14) {
throw new CodeGenerationException(var14);
}
}
}