类加载器
源码:
根加载器&&扩展类加载器&&系统加载器&&自定义加载器
自定义加载器
public class CousmerClassLoad extends ClassLoader {
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException{
Class<?> aclass = null;
try {
FileInputStream fileInputStream = new FileInputStream(new File(name));
byte[] bytes = new byte[fileInputStream.available()];
fileInputStream.read(bytes);
aclass = defineClass(bytes, 0, bytes.length);
System.out.println(aclass.getName());
return aclass;
} catch (Exception e) {
throw new ClassNotFoundException();
}
}
public static void main(String[] args) throws ClassNotFoundException {
CousmerClassLoad classLoad = new CousmerClassLoad();
Class<?> aClass = classLoad.findClass("D:\\project\\_11111\\target\\classes\\com\\pingan\\Animal.class");
System.out.println(aClass.getName());
}
}
类加载器路径
类加载顺序
上图:
直接分析class字节码文件:
上图为类初始化执行的指令,可以看出执行了静态块,以及给静态成员变量赋值!
什么时候给普通成员变量赋值,以及执行普通代码块?看下图
查看字节码idea插件
授人以鱼不如授人以渔!