阅读本文大概需要 5 分钟。
@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 test44(){
try {
Class.forName(“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()
}
}
推荐阅读:
微信扫描二维码,关注我的公众号
朕已阅
文章收集整理于网络,请勿商用,仅供个人学习使用,如有侵权,请联系作者删除,如若转载,请注明出处:http://www.cxyroad.com/1580.html