这是源码,你看一下。
/**
* Returns the <code>Class</code> object associated with the class or
* interface with the given string name. Invoking this method is
* equivalent to:
*
* <blockquote><pre>
* Class.forName(className, true, currentLoader)
* </pre></blockquote>
*
* where <code>currentLoader</code> denotes the defining class loader of
* the current class.
*
* <p> For example, the following code fragment returns the
* runtime <code>Class</code> descriptor for the class named
* <code>java.lang.Thread</code>:
*
* <blockquote><pre>
* Class t = Class.forName("java.lang.Thread")
* </pre></blockquote>
* <p>
* A call to <tt>forName("X")</tt> causes the class named
* <tt>X</tt> to be initialized.
*
* @param className the fully qualified name of the desired class.
* @return the <code>Class</code> object for the class with the
* specified name.
* @exception LinkageError if the linkage fails
* @exception ExceptionInInitializerError if the initialization provoked
* by this method fails
* @exception ClassNotFoundException if the class cannot be located
*/
public static Class forName(String className)
throws ClassNotFoundException {
return forName0(className, true, ClassLoader.getCallerClassLoader());
}
/**
* Returns the <code>Class</code> object associated with the class or
* interface with the given string name, using the given class loader.
* Given the fully qualified name for a class or interface (in the same
* format returned by <code>getName</code>) this method attempts to
* locate, load, and link the class or interface. The specified class
* loader is used to load the class or interface. If the parameter
* <code>loader</code> is null, the class is loaded through the bootstrap
* class loader. The class is initialized only if the
* <code>initialize</code> parameter is <code>true</code> and if it has
* not been initialized earlier.
*
* <p> If <code>name</code> denotes a primitive type or void, an attempt
* will be made to locate a user-defined class in the unnamed package whose
* name is <code>name</code>. Therefore, this method cannot be used to
* obtain any of the <code>Class</code> objects representing primitive
* types or void.
*
* <p> If <code>name</code> denotes an array class, the component type of
* the array class is loaded but not initialized.
*
* <p> For example, in an instance method the expression:
*
* <blockquote><pre>
* Class.forName("Foo")
* </pre></blockquote>
*
* is equivalent to:
*
* <blockquote><pre>
* Class.forName("Foo", true, this.getClass().getClassLoader())
* </pre></blockquote>
*
* Note that this method throws errors related to loading, linking or
* initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The
* Java Language Specification</em>.
* Note that this method does not check whether the requested class
* is accessible to its caller.
*
* <p> If the <code>loader</code> is <code>null</code>, and a security
* manager is present, and the caller's class loader is not null, then this
* method calls the security manager's <code>checkPermission</code> method
* with a <code>RuntimePermission("getClassLoader")</code> permission to
* ensure it's ok to access the bootstrap class loader.
*
* @param name fully qualified name of the desired class
* @param initialize whether the class must be initialized
* @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
*/
public static Class forName(String name, boolean initialize,
ClassLoader loader)
throws ClassNotFoundException
{
if (loader == null) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
ClassLoader ccl = ClassLoader.getCallerClassLoader();
if (ccl != null) {
sm.checkPermission(ClassLoader.getGetClassLoaderPerm());
}
}
}
return forName0(name, initialize, loader);
} /** Called after security checks have been made. */
private static native Class forName0(String name, boolean initialize,
ClassLoader loader)
throws ClassNotFoundException;
/**
* Returns the <code>Class</code> object associated with the class or
* interface with the given string name. Invoking this method is
* equivalent to:
*
* <blockquote><pre>
* Class.forName(className, true, currentLoader)
* </pre></blockquote>
*
* where <code>currentLoader</code> denotes the defining class loader of
* the current class.
*
* <p> For example, the following code fragment returns the
* runtime <code>Class</code> descriptor for the class named
* <code>java.lang.Thread</code>:
*
* <blockquote><pre>
* Class t = Class.forName("java.lang.Thread")
* </pre></blockquote>
* <p>
* A call to <tt>forName("X")</tt> causes the class named
* <tt>X</tt> to be initialized.
*
* @param className the fully qualified name of the desired class.
* @return the <code>Class</code> object for the class with the
* specified name.
* @exception LinkageError if the linkage fails
* @exception ExceptionInInitializerError if the initialization provoked
* by this method fails
* @exception ClassNotFoundException if the class cannot be located
*/
public static Class forName(String className)
throws ClassNotFoundException {
return forName0(className, true, ClassLoader.getCallerClassLoader());
}
/**
* Returns the <code>Class</code> object associated with the class or
* interface with the given string name, using the given class loader.
* Given the fully qualified name for a class or interface (in the same
* format returned by <code>getName</code>) this method attempts to
* locate, load, and link the class or interface. The specified class
* loader is used to load the class or interface. If the parameter
* <code>loader</code> is null, the class is loaded through the bootstrap
* class loader. The class is initialized only if the
* <code>initialize</code> parameter is <code>true</code> and if it has
* not been initialized earlier.
*
* <p> If <code>name</code> denotes a primitive type or void, an attempt
* will be made to locate a user-defined class in the unnamed package whose
* name is <code>name</code>. Therefore, this method cannot be used to
* obtain any of the <code>Class</code> objects representing primitive
* types or void.
*
* <p> If <code>name</code> denotes an array class, the component type of
* the array class is loaded but not initialized.
*
* <p> For example, in an instance method the expression:
*
* <blockquote><pre>
* Class.forName("Foo")
* </pre></blockquote>
*
* is equivalent to:
*
* <blockquote><pre>
* Class.forName("Foo", true, this.getClass().getClassLoader())
* </pre></blockquote>
*
* Note that this method throws errors related to loading, linking or
* initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The
* Java Language Specification</em>.
* Note that this method does not check whether the requested class
* is accessible to its caller.
*
* <p> If the <code>loader</code> is <code>null</code>, and a security
* manager is present, and the caller's class loader is not null, then this
* method calls the security manager's <code>checkPermission</code> method
* with a <code>RuntimePermission("getClassLoader")</code> permission to
* ensure it's ok to access the bootstrap class loader.
*
* @param name fully qualified name of the desired class
* @param initialize whether the class must be initialized
* @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
*/
public static Class forName(String name, boolean initialize,
ClassLoader loader)
throws ClassNotFoundException
{
if (loader == null) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
ClassLoader ccl = ClassLoader.getCallerClassLoader();
if (ccl != null) {
sm.checkPermission(ClassLoader.getGetClassLoaderPerm());
}
}
}
return forName0(name, initialize, loader);
} /** Called after security checks have been made. */
private static native Class forName0(String name, boolean initialize,
ClassLoader loader)
throws ClassNotFoundException;
解决方案 »
- JS怎么判断多个对象相等
- 将带有第三方JAR的项目打包成JAR后,使用时找不到第三方包
- 用MyEclipse写jsp出现的莫名其妙的错
- 请问:如何将媒体播放器嵌入应用界面当中?
- doGet()与doPost()方法的区别
- 我有一套JSP源程序,想修改一下为我所用,但是有点麻烦
- 帮看看这个怎么算?
- 在录入记录之前怎样处理“判断库中是否已有相同记录”这个过程?急求
- 我装的Mysql怎么没有test与mysql库啊。
- 发送邮件发生错误:java.lang.SecurityException: Access to default session denied
- java中是否有缺省参数这个东西啊
- 菜鸟提问:怎样做一个在窗体关闭之前的询问对话框?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~运行时动态得到一个类的实例.
* @param initialize 是否需要初始化
* @param loader 指定类装载器
* @return 想要得到的类的Class实例
注意Class.forName的返回
forName0(name, initialize, loader); 这是一个native method,将会返回一个名字为name的类的Class类型的实例,对返回的实例进行newInstance操作就得到一个符合这个Class类型的类的实例。
Class是一个描述类的属性的类。
例如:
String name = "Integer";
Class c1 = Class.forName(name);//获得一个Integer类的Class实例。
Integer int1 = c1.newInstance;//注意newInstance返回是Object类型的。
假如你要使用Integer的ParseInt方法,你可以这样
Class.forName("Integer").newtInstance.parseInt(s);
又如:
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance;
是在系统中装载了一个驱动类。