类装载器负责寻找类的字节码文件并构造出jvm中对应的对象组件。要经过以下步骤:
1、查找并导入class字节码文件
2、执行校验,解析步骤
3、初始化:对类的静态变量、静态代码块进行初始化工作。
类的初始化工作由ClassLoader以及其子类负责:ClassLoader-->ExtClassLoader-->AppClassLoader。
ClassLoader 负责加载JRE的核心类库,ExtClassLoader负责加载JRE的扩展类库,AppClassLoader负责加载应用Classpath下的jar文件。
public class ClassLoaderTest {
public static void main(String[] args) {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
System.out.println("currrent thread" + cl);
System.out.println("parent thread"+cl.getParent());
//根类装载器在java中找不到,native方法实现
System.out.println("parent parent thread"+cl.getParent().getParent());
}
}
输出信息
currrent threadsun.misc.Launcher$AppClassLoader@6c68bcef
parent threadsun.misc.Launcher$ExtClassLoader@504c2683
parent parent threadnull
类的加载过程中采用“全盘负责委托机制”:委托机制是指类加载过程中优先又父类进行加载,如父类找不到则再由子类加载,全盘负责指:装载类时,其依赖以及引用的类优先用此ClassLoader加载。