往期精选
其他
【003期】Java 中 Class.forName 和 ClassLoader 到底有啥区别?
前言
解释
@CallerSensitive
public static Class<?> forName(String className) throws ClassNotFoundException {
Class<?> caller = Reflection.getCallerClass();
return forName0(className, true, ClassLoader.getClassLoader(caller), caller);
}
/* @param name fully qualified name of the desired class
* @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);
}
举例
public class ClassForName {
//静态代码块
static {
System.out.println("执行了静态代码块");
}
//静态变量
private static String staticFiled = staticMethod();
//赋值静态变量的静态方法
public static String staticMethod(){
System.out.println("执行了静态方法");
return "给静态字段赋值了";
}
}
@Test
public void test45(){
try {
ClassLoader.getSystemClassLoader().loadClass("com.eurekaclient2.client2.ClassForName");
System.out.println("#########-------------结束符------------##########");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
执行了静态代码块
执行了静态方法
#########-------------结束符------------##########
@Test
public void test45(){
try {
ClassLoader.getSystemClassLoader().loadClass("com.eurekaclient2.client2.ClassForName");
System.out.println("#########-------------结束符------------##########");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
#########-------------结束符------------##########
应用场景
public class Driver extends NonRegisteringDriver implements java.sql.Driver {
// ~ 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()
}
}
等找到工作后,会总结自己面经的。
作者:纪莫
www.cnblogs.com/jimoer/p/9185662.html