JAVA知识:Class.forName的含义
来源:互联网 发布:软件管家安卓版 编辑:程序博客网 时间:2024/05/20 09:22
导读:
Class aClass = Class.forName(xxx.xx.xx);
Object anInstance = aClass.newInstance();
Class.forName("").newInstance()返回的是object
but there is some limit for this method to create instance
that is your class constructor should no contain parameters,
and you should cast the instance manually.
Class Driver{
protected static Driver current;
public static Driver getDriver(){
return current;
}
}
Class MyDriver extends Driver{
static{
Driver.current=new MyDriver();
}
MyDriver(){}
}
bitsCN~com
BBS.bitsCN.com网管论坛
用时:
bbs.bitsCN.com
Class.forName("MyDriver");
Driver d=Driver.getDriver();c
bbs.bitsCN.com
www_bitscn_com中国.网管联盟
有的jdbc连接数据库的写法里是Class.forName(xxx.xx.xx);而有一些:Class.forName(xxx.xx.xx)。newInstance(),为什么会有这两种写法呢? www_bitscn_com中国.网管联盟
bitsCN.nET*中国网管博客
Class.forName(xxx.xx.xx) 返回的是一个类,
bitsCN~com
。newInstance() 后才创建一个对象 bitsCN~com
Class.forName(xxx.xx.xx);的作用是要求JVM查找并加载指定的类,也就是说JVM会执行该类的静态代码段
www@bitscn@com
在JDBC规范中明确要求这个Driver类必须向DriverManager注册自己,即任何一个JDBC Driver的Driver类的代码都必须类似如下:
bitsCN#com中国网管联盟
public class MyJDBCDriver implements Driver {
static {
DriverManager.registerDriver(new MyJDBCDriver());
}
}
[bitsCN_com]
bbs.bitsCN.com中国网管论坛
所以我们在使用JDBC时只需要Class.forName(XXX.XXX);就可以了 www_bitscn_com中国.网管联盟
在JDBC驱动中,有一块静态代码,也叫静态初始化块,它执行的时间是当class调入到内存中就执行(你可以想像成,当类调用到内存后就执行一个方法)。所以很多人把jdbc driver调入到内存中,再实例化对象是没有意义的。 [bitsCN_com]
本文转自
http://www.bitscn.com/java/base/200709/110155.html
Class aClass = Class.forName(xxx.xx.xx);
Object anInstance = aClass.newInstance();
Class.forName("").newInstance()返回的是object
but there is some limit for this method to create instance
that is your class constructor should no contain parameters,
and you should cast the instance manually.
Class Driver{
protected static Driver current;
public static Driver getDriver(){
return current;
}
}
Class MyDriver extends Driver{
static{
Driver.current=new MyDriver();
}
MyDriver(){}
}
bitsCN~com
BBS.bitsCN.com网管论坛
用时:
bbs.bitsCN.com
Class.forName("MyDriver");
Driver d=Driver.getDriver();c
bbs.bitsCN.com
www_bitscn_com中国.网管联盟
有的jdbc连接数据库的写法里是Class.forName(xxx.xx.xx);而有一些:Class.forName(xxx.xx.xx)。newInstance(),为什么会有这两种写法呢? www_bitscn_com中国.网管联盟
bitsCN.nET*中国网管博客
Class.forName(xxx.xx.xx) 返回的是一个类,
bitsCN~com
。newInstance() 后才创建一个对象 bitsCN~com
Class.forName(xxx.xx.xx);的作用是要求JVM查找并加载指定的类,也就是说JVM会执行该类的静态代码段
www@bitscn@com
在JDBC规范中明确要求这个Driver类必须向DriverManager注册自己,即任何一个JDBC Driver的Driver类的代码都必须类似如下:
bitsCN#com中国网管联盟
public class MyJDBCDriver implements Driver {
static {
DriverManager.registerDriver(new MyJDBCDriver());
}
}
[bitsCN_com]
bbs.bitsCN.com中国网管论坛
所以我们在使用JDBC时只需要Class.forName(XXX.XXX);就可以了 www_bitscn_com中国.网管联盟
在JDBC驱动中,有一块静态代码,也叫静态初始化块,它执行的时间是当class调入到内存中就执行(你可以想像成,当类调用到内存后就执行一个方法)。所以很多人把jdbc driver调入到内存中,再实例化对象是没有意义的。 [bitsCN_com]
本文转自
http://www.bitscn.com/java/base/200709/110155.html
- JAVA知识:Class.forName的含义
- Class.forName的含义
- Class.forName的含义
- Class.forName的含义
- Class.forName的含义
- java中Class.forName的含义
- java中Class.forName的含义
- java中Class.forName的含义
- JAVA中CLASS.FORNAME的含义
- java中Class.forName的含义
- JAVA中CLASS.FORNAME的含义
- java中Class.forName的含义
- java中Class.forName的含义
- JAVA中CLASS.FORNAME的含义
- java中Class.forName的含义
- java中Class.forName的含义
- JAVA中CLASS.FORNAME的含义
- 理解Class.forName的含义
- 用java监视系统进程的两个解决方案
- Generic host process for win32 services 遇到问题需要关闭!
- Interview Experience
- 开发JAVA第一步,教你选好JSP虚拟空间
- 敏捷开发技巧:以用户故事管理项目
- JAVA知识:Class.forName的含义
- Java的字符串操作
- 如何下手学JAVA
- JAVA 30个基本知识
- 如何运行Java程序和设置CLASSPAT
- 编程基础:Java中的数据结构
- 如何开始学习jsp
- 基础应用:java web三层架构程序实例(组图)
- Java继承时构造函数的调用