What does Class.forName(); do?
2010-12-27 16:23
513 查看
Question:
What exactly does the
statement do? Why is this construct encountered only with JDBC? (I have not seen any code that uses
in other contexts.)
Answer:
Every object has a corresponding class object. This class object instance is shared among all instances of that class type.
loads and returns the class object for the given class name. So, for example, if you write:
the JVM will load and return the class object for
. Class objects play an important role in reflection. For example, you may call:
In the example above,
goes out and instantiates an object for class if it has a no arguments constructor. You can also use the class object to
obtain field, method, and interface information.
The example given above is a bit contrived.
However, let's say you define an interface that provides a proxy to some
backend
server. Your program knows how to manipulate the
interface; it does not, however, know which interface implementation to
instantiate
at compile time. Instead, you provide that class
name as a command line argument. Your program can use reflection to go
out
and instantiate that object.
In the case of JDBC, the
loads the JDBC driver into the JVM.
What exactly does the
Class.forName();
statement do? Why is this construct encountered only with JDBC? (I have not seen any code that uses
Class.forName()
in other contexts.)
Answer:
Every object has a corresponding class object. This class object instance is shared among all instances of that class type.
Class.forName()
loads and returns the class object for the given class name. So, for example, if you write:
Class object = Class.forName("java.io.FileNotFoundException");
the JVM will load and return the class object for
FileNotFoundException
. Class objects play an important role in reflection. For example, you may call:
try
{
Class object = Class.forName("java.io.FileNotFoundException");Object ex = object.newInstance();
}
catch (ClassNotFoundException ex) {}
catch (InstantiationException ex) {}
catch (IllegalAccessException ex) {}
In the example above,
newInstance()
goes out and instantiates an object for class if it has a no arguments constructor. You can also use the class object to
obtain field, method, and interface information.
The example given above is a bit contrived.
However, let's say you define an interface that provides a proxy to some
backend
server. Your program knows how to manipulate the
interface; it does not, however, know which interface implementation to
instantiate
at compile time. Instead, you provide that class
name as a command line argument. Your program can use reflection to go
out
and instantiate that object.
In the case of JDBC, the
Class.forName()
loads the JDBC driver into the JVM.
相关文章推荐
- What does Class.forname method do?
- What does Class.forname method do?
- What does dirname(__FILE__) and basename(dirname(__FILE__)) do?
- What does the CS_CLASSDC & CS_OWNDC class style do?
- What does if __name__ == “__main__”: do?
- Class.forName("java.lang.String")的作用?
- java反射(1) 之class.forName(""), 类名.class, 对象.getClass()区别
- 【转】Class.forName 与 new 的区别
- Class.forName()用法详解
- Java 反射中,class.forName()和classLoader的区别
- 关于android studio中使用class.forname()方法动态获取类实例报NO CLASS FOUND异常的几种处理方法
- Class.forName()用法 c26a 详解
- java中动态加载类 Class.forName
- class does not name a type
- what to do for the future?
- Class.forName的作用以及为什么要用它
- Class.forName
- Class.forName的作用以及为什么要用它
- Class.forName()与load.loadClass()比较
- Class.forName