其他
Class.forName 和 ClassLoader 到底有啥区别?
前言
解释
public static Class<?> forName(String className)
throws ClassNotFoundException {
Class<?> caller = Reflection.getCallerClass();
return forName0(className, true, ClassLoader.getClassLoader(caller), caller);
}
* @param initialize if {@code true} the class will be initialized.
* See Section 12.4 of <em>The Java Language Specification</em>.
* @param loader class loader from which the class must be loaded
* @return class object representing the desired class
*
* @exception LinkageError if the linkage fails
* @exception ExceptionInInitializerError if the initialization provoked
* by this method fails
* @exception ClassNotFoundException if the class cannot be located by
* the specified class loader
*
* @see java.lang.Class#forName(String)
* @see java.lang.ClassLoader
* @since 1.2 */
@CallerSensitive
public static Class<?> forName(String name, boolean initialize,
ClassLoader loader)
throws ClassNotFoundException
{
Class<?> caller = null;
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
// Reflective call to get caller class is only needed if a security manager
// is present. Avoid the overhead of making this call otherwise.
caller = Reflection.getCallerClass();
if (sun.misc.VM.isSystemDomainLoader(loader)) {
ClassLoader ccl = ClassLoader.getClassLoader(caller);
if (!sun.misc.VM.isSystemDomainLoader(ccl)) {
sm.checkPermission(
SecurityConstants.GET\_CLASSLOADER\_PERMISSION);
}
}
}
return forName0(name, initialize, loader, caller);
}
举例
//静态代码块
static {
System.out.println("执行了静态代码块");
}
//静态变量
private static String staticFiled = staticMethod();
//赋值静态变量的静态方法
public static String staticMethod(){
System.out.println("执行了静态方法");
return "给静态字段赋值了";
}
}
public void test45(){
try {
ClassLoader.getSystemClassLoader().loadClass("com.eurekaclient2.client2.ClassForName");
System.out.println("#########-------------结束符------------##########");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void test45(){
try {
ClassLoader.getSystemClassLoader().loadClass("com.eurekaclient2.client2.ClassForName");
System.out.println("#########-------------结束符------------##########");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
应用场景
// ~ Static fields/initializers
// ---------------------------------------------
//
// Register ourselves with the DriverManager
//
static {
try {
java.sql.DriverManager.registerDriver(new Driver());
} catch (SQLException E) {
throw new RuntimeException("Can't register driver!");
}
}
// ~ Constructors
// -----------------------------------------------------------
/**
* Construct a new driver and register it with DriverManager
*
* @throws SQLException
* if a database error occurs.
*/
public Driver() throws SQLException {
// Required for Class.forName().newInstance() }
}
---END--- 重磅!鱼哥微信好友坑位限时开放啦!
福利来啦!扫码直接加鱼哥微信号,送你一份刷题指南和面试手册,不仅可以围观鱼哥平时所思和复盘的内容。还可以帮你免费内推大厂,技术交流,一起探索职场突围,收入突围,技术突围。一定要备注:开发方向+地点+学校/公司+昵称(如Java开发+上海+拼夕夕+猴子)
▲长按加鱼哥微信,赶紧上车
福利来啦!扫码直接加鱼哥微信号,送你一份刷题指南和面试手册,不仅可以围观鱼哥平时所思和复盘的内容。还可以帮你免费内推大厂,技术交流,一起探索职场突围,收入突围,技术突围。一定要备注:开发方向+地点+学校/公司+昵称(如Java开发+上海+拼夕夕+猴子)