Class.forName(),Class.forName().newInstance() ,New ,类名.class,,class.forName(),,getClass()

   在Java开发特别是数据库开发中,经常会用到Class.forName( )这个方法。通过查询Java Documentation我们会发现使用Class.forName( )静态方法的目的是为了动态加载类。在加载完成后,一般还要调用Class下的newInstance( )静态方法来实例化对象以便操作。因此,单单使用Class.forName( )是动态加载类是没有用的,其最终目的是为了实例化对象。
   这里有必要提一下就是Class下的newInstance()和new有什么区别?,首先,newInstance( )是一个方法,而new是一个关键字,其次,Class下的newInstance()的使用有局限,因为它生成对象只能调用无参的构造函数,而使用 new关键字生成对象没有这个限制。
   好,到此为止,我们总结如下:
   Class.forName("")返回的是类
   Class.forName("").newInstance()返回的是object
   有数据库开发经验朋友会发现,为什么在我们加载数据库驱动包的时候有的却没有调用newInstance( )方法呢?即有的jdbc连接数据库的写法里是Class.forName(xxx.xx.xx);而有一 些:Class.forName(xxx.xx.xx).newInstance(),为什么会有这两种写法呢?
   刚才提到,Class.forName("");的作用是要求JVM查找并加载指定的类,如果在类中有静态初始化器的话,JVM必然会执行该类的静态代码 段。而在JDBC规范中明确要求这个Driver类必须向DriverManager注册自己,即任何一个JDBC Driver的 Driver类的代码都必须类似如下:
  
1 public class MyJDBCDriver implements Driver {
2    static {
3      DriverManager.registerDriver(new MyJDBCDriver());
4    }
5 }

 

既然在静态初始化器的中已经进行了注册,所以我们在使用JDBC时只需要Class.forName(XXX.XXX);就可以了。
 
贴出Proxool 连接池的静态初始化方法:
 1 public class ProxoolDriver implements Driver {
 2     private static final Log LOG = LogFactory.getLog(ProxoolDriver.class);
 3     static {
 4         try {
 5             DriverManager.registerDriver(new ProxoolDriver());
 6         } catch (SQLException e) {
 7             System.out.println(e.toString());
 8         }
 9     }
10 }

 

1: Class cl=A.class;  

                  JVM将使用类A的类装载器, 将类A装入内存(前提是:类A还没有装入内存),不对类A做类的初始化工作.返回类A的Class的对象。

2:Class cl=对象引用o.getClass();

                  返回引用o运行时真正所指的对象(因为:子对象的引用可能会赋给父对象的引用变量中)所属的类的Class的对象 。

3:Class.forName("类名");

                  .装入类A,并做类的初始化

.getClass()是动态的,其余是静态的。

.class和class.forName()只能返回类内field的默认值,getClass可以返回当前对象中field的最新值

Class.forName() 返回的是一个类,.newInstance() 后才创建一个对象,Class.forName()的作用是要求JVM查找并加载指定的类,也就是说JVM会执行该类的

1 public class Person {
2     private String name = "Alfira";
3     public void getName() {
4         System.out.println(name);
5     }
6     public void setName(String name, int a) {
7         this.name = name + a;
8     }
9 }
 1 import java.lang.reflect.Method;
 2 
 3 public class Test {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         show("yerasel.Person");
10     }
11 
12     private static void show(String name) {
13         try {
14             // JVM将使用类A的类装载器,将类A装入内存(前提是:类A还没有装入内存),不对类A做类的初始化工作
15             Class classtype3 = Person.class;
16             // 获得classtype中的方法
17             Method getMethod3 = classtype3.getMethod("getName", new Class[] {});
18             Class[] parameterTypes3 = { String.class, int.class };
19             Method setMethod3 = classtype3
20                     .getMethod("setName", parameterTypes3);
21 
22             // 实例化对象,因为这一句才会输出“静态初始化”以及“初始化”
23             Object obj3 = classtype3.newInstance();
24             // 通过实例化后的对象调用方法
25             getMethod3.invoke(obj3); // 获取默认值
26             setMethod3.invoke(obj3, "Setting new ", 3); // 设置
27             getMethod3.invoke(obj3); // 获取最新
28             System.out.println("----------------");
29 
30             // 返回运行时真正所指的对象
31             Person p = new Person();
32             Class classtype = p.getClass();// Class.forName(name);
33             // 获得classtype中的方法
34             Method getMethod = classtype.getMethod("getName", new Class[] {});
35             Class[] parameterTypes = { String.class, int.class };
36             Method setMethod = classtype.getMethod("setName", parameterTypes);
37             getMethod.invoke(p);// 获取默认值
38             setMethod.invoke(p, "Setting new ", 1); // 设置
39             getMethod.invoke(p);// 获取最新
40             System.out.println("----------------");
41 
42             // 装入类,并做类的初始化
43             Class classtype2 = Class.forName(name);
44             // 获得classtype中的方法
45             Method getMethod2 = classtype2.getMethod("getName", new Class[] {});
46             Class[] parameterTypes2 = { String.class, int.class };
47             Method setMethod2 = classtype2
48                     .getMethod("setName", parameterTypes2);
49             // 实例化对象
50             Object obj2 = classtype2.newInstance();
51             // 通过实例化后的对象调用方法
52             getMethod2.invoke(obj2); // 获取默认值
53             setMethod2.invoke(obj2, "Setting new ", 2); // 设置
54             getMethod2.invoke(obj2); // 获取最新
55 
56             System.out.println("----------------");
57 
58         } catch (Exception e) {
59             System.out.println(e);
60         }
61     }
62 }

 

posted @ 2013-10-05 14:37  曾先森在努力  阅读(1116)  评论(0编辑  收藏  举报