标签:style blog class c code java
在数据库调用常见 Class.forName("com.mysql.jdbc.Driver");语句,这是用于加载数据库的此类.
此类的源码是
1 public class Driver extends NonRegisteringDriver implements java.sql.Driver { 2 3 static { 4 try { 5 java.sql.DriverManager.registerDriver(new Driver()); 6 } catch (SQLException E) { 7 throw new RuntimeException("Can‘t register driver!"); 8 } 9 } 10 11 12 public Driver() throws SQLException { 13 // Required for Class.forName().newInstance() 14 } 15 }
加载此类的目的是为了执行依赖此类的静态方法。也可以通过创建此类的对象的方式来 动态加载此类以达到实现此类的静态方法。
不管加载此类还是 创建此类的对象目的都是为了执行此类的静态方法。
测试加载类并且创建静态方法
1 package cn.itcast.util; 2 3 public class JdbcUtil { 4 5 static 6 { 7 System.out.println("load this class"); 8 } 9 10 public static void main (String[] args) throws ClassNotFoundException 11 { 12 //aa b=new aa(); 13 Class.forName("cn.itcast.util.aa"); 14 System.out.println("xxxxxxxx"); 15 } 16 } 17 class aa 18 { 19 static 20 { 21 System.out.println("load this class aaaaaaaaaa"); 22 } 23 }
标签:style blog class c code java
原文地址:http://www.cnblogs.com/friends-wf/p/3739509.html