答案:不会!
常见类加载器:
Loading(加载)
Bootstrap ClassLoader 负责java类库 java.*
Extension ClassLoader 负责加载java扩展类库 javax.*
Application ClassLoader 加载程序所在目录 即classpath路径下的类文件
类加载顺序:
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 {
//使用bootstrap类加载器加载
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;
}
}
问题:自定义java.lang.Object类项目是否可以引用?
系统Object类应该有Bootstrapclassloader加载,而根据上述源码,查找顺序为Extension
ClassLoader -> bootstrap ClassLader->Application ClassLoader 中查找
因此Object类会加载到java类库下的Object类,而不是自己定义。
而如果其他定义,如引入的POI类包,会先加载自己定义的常量类
找不到回去jar包中找。
网友评论