1.代码解析
- 我们先来看一下这段代码,注意包名和类名,此java文件编译通过,但是运行会报错
package java.util;
public class List {
public static void main(String[] args) {
System.out.println("List类");
}
}
2. 这是为什么呢?我们来看一段源码,这是ClassLoader的loadClass方 法。解释就是第一次加载一个类时,类加载器会调用父加载器去加载这个类
protected Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException
{
synchronized (getClassLoadingLock(name)) {
// First, check if the class has already been loaded
Class<?> c = findLoadedClass(name);
if (c == null) {
long t0 = System.nanoTime();
try {
if (parent != null) {
c = parent.loadClass(name, false);
} else {
c = findBootstrapClassOrNull(name);
}
} catch (ClassNotFoundException e) {
// ClassNotFoundException thrown if class not found
// from the non-null parent class loader
}
if (c == null) {
// If still not found, then invoke findClass in order
// to find the class.
long t1 = System.nanoTime();
c = findClass(name);
// this is the defining class loader; record the stats
sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
sun.misc.PerfCounter.getFindClasses().increment();
}
}
if (resolve) {
resolveClass(c);
}
return c;
}
}
2. 使用不同类加载器加载对象
这段代码表示,我们使用系统自带的类加载器,和使用自定义类加载器去加载同一个类,比较得到的结果。
package cn.mytest;
import java.io.IOException;
import java.io.InputStream;
public class ClassLoaderTest {
public static void main(String[] args) throws ClassNotFoundException {
//自定义类加载器
ClassLoader loader = new ClassLoader() {
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
try {
String fileName = name.substring(name.lastIndexOf(".")+1)+".class";
InputStream is = getClass().getResourceAsStream(fileName);
if(is == null){
return super.loadClass(name);
}
byte[] b = new byte[is.available()];
is.read(b);
return defineClass(name,b,0,b.length);
}catch (IOException e){
throw new ClassNotFoundException();
}
}
};
String className = "cn.mytest.ClassLoaderTest";
//使用系统默认加载器
Object obj1 = ClassLoaderTest.class.getClassLoader().loadClass(className);
//使用自定义加载器
Object obj2 = loader.loadClass(className);
System.out.println(obj1 == obj2);
System.out.println(obj1.equals(obj2));
}
}
3.结果
结果说明使用系统自带的类加载器,和使用自定义类加载器加载同一对象,得到的对象地址不一样,内容也不一样,即完全不同