其中scl为本类中声明的变量,用来保存系统类加载器,
private static ClassLoader scl;
然后通过在getSystemClassLoader方法时,通过初始化方法来给其赋值并返回
public static ClassLoader getSystemClassLoader() {
initSystemClassLoader();
if (scl == null) {
return null;
}
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
checkClassLoaderPermission(scl, Reflection.getCallerClass());
}
return scl;
}
initSystemClassLoader()用来完成scl的赋值:
private static synchronized void initSystemClassLoader() {
if (!sclSet) {
if (scl != null)
throw new IllegalStateException("recursive invocation");
sun.misc.Launcher l = sun.misc.Launcher.getLauncher();
if (l != null) {
Throwable oops = null;
scl = l.getClassLoader();
try {
scl = AccessController.doPrivileged(
new SystemClassLoaderAction(scl));
} catch (PrivilegedActionException pae) {
oops = pae.getCause();
if (oops instanceof InvocationTargetException) {
oops = oops.getCause();
}
}
if (oops != null) {
if (oops instanceof Error) {
throw (Error) oops;
} else {
// wrap the exception
throw new Error(oops);
}
}
}
sclSet = true;
}
}
其中sun.misc.Launcher l = sun.misc.Launcher.getLauncher();定义了一个Launcher类,该类有个成员变量classLoader来保存系统类加载器,其构造方法给classLoader赋值。
通过scl = l.getClassLoader();拿到了Launcher类中的classLoader成员变量