new Instance()方法和new关键

news/2024/5/18 22:22:33 标签: class, jdbc, jvm, constructor, parameters, string
class="baidu_pl">
class="article_content clearfix">
class="htmledit_views">

 

在初始化一个类,生成一个实例的时候,newInstance()方法和new关键字除了一个是方法,一个是关键字外,最主要有什么区别?它们的区别在于创建对象的方式不一样,前者是使用类加载机制,后者是创建一个新类。那么为什么会有两种创建对象方式?这主要考虑到软件的可伸缩、可扩展和可重用等软件设计思想。

Java中工厂模式经常使用newInstance()方法来创建对象,因此从为什么要使用工厂模式上可以找到具体答案。 例如:
class c = Class.forName(“Example”);
factory = (ExampleInterface)c.newInstance();

其中ExampleInterface是Example的接口,可以写成如下形式:
String className = "Example";
class c = Class.forName(className);
factory = (ExampleInterface)c.newInstance();

进一步可以写成如下形式:
String className = readfromXMlConfig;//从xml 配置文件中获得字符串
class c = Class.forName(className);
factory = (ExampleInterface)c.newInstance();

上面代码已经不存在Example的类名称,它的优点是,无论Example类怎么变化,上述代码不变,甚至可以更换Example的兄弟类Example2 , Example3 , Example4……,只要他们继承ExampleInterface就可以。

从JVM的角度看,我们使用关键字new创建一个类的时候,这个类可以没有被加载。但是使用newInstance()方法的时候,就必须保证:1、这个类已经加载;2、这个类已经连接了。而完成上面两个步骤的正是Class的静态方法forName()所完成的,这个静态方法调用了启动类加载器,即加载java API的那个加载器。

现在可以看出,newInstance()实际上是把new这个方式分解为两步,即首先调用Class加载方法加载某个类,然后实例化。 这样分步的好处是显而易见的。我们可以在调用class的静态加载方法forName时获得更好的灵活性,提供给了一种降耦的手段。

最后用最简单的描述来区分new关键字和newInstance()方法的区别:
newInstance: 弱类型。低效率。只能调用无参构造。
new: 强类型。相对高效。能调用任何public构造。

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(){}
}

用时:
Class.forName("MyDriver");
Driver d=Driver.getDriver();

有的jdbc连接数据库的写法里是Class.forName(xxx.xx.xx);而有一些:Class.forName(xxx.xx.xx).newInstance(),为什么会有这两种写法呢?

Class.forName(xxx.xx.xx) 返回的是一个类,
.newInstance() 后才创建一个对象

Class.forName(xxx.xx.xx);的作用是要求JVM查找并加载指定的类,也就是说JVM会执行该类的静态代码段

在JDBC规范中明确要求这个Driver类必须向DriverManager注册自己,即任何一个JDBC Driver的Driver类的代码都必须类似如下:
public class MyJDBCDriver implements Driver {
static {
DriverManager.registerDriver(new MyJDBCDriver());
}
}

所以我们在使用JDBC时只需要Class.forName(XXX.XXX);就可以了

we just want to load the driver to jvm only, but not need to user the instance of driver, so call Class.forName(xxx.xx.xx) is enough, if you call Class.forName(xxx.xx.xx).newInstance(), the result will same as calling Class.forName(xxx.xx.xx), because Class.forName(xxx.xx.xx).newInstance() will load driver first, and then create instance, but the instacne you will never use in usual, so you need not to create it.

在JDBC驱动中,有一块静态代码,也叫静态初始化块,它执行的时间是当class调入到内存中就执行(你可以想像成,当类调用到内存后就执行一个方法)。所以很多人把jdbc driver调入到内存中,再实例化对象是没有意义的。

资源引用:

http://hi.baidu.com/wangyuege/blog/item/3e9fd8a2cc4e16accbefd080.html


http://www.niftyadmin.cn/n/1051886.html

相关文章

洛谷P1567 统计天数

题目背景统计天数题目描述炎热的夏日&#xff0c;KC非常的不爽。他宁可忍受北极的寒冷&#xff0c;也不愿忍受厦门的夏天。最近&#xff0c;他开始研究天气的变化。他希望用研究的结果预测未来的天气。经历千辛万苦&#xff0c;他收集了连续N&#xff08;1<N<10^7&#x…

200 行 C 代码实现插件式 NOSQL 存储服务器(一) - Haippy - 博客园

200 行 C 代码实现插件式 NOSQL 存储服务器(一)2012-11-24 22:30 by Haippy, 837 阅读, 1 评论, 收藏, 编辑初一看&#xff0c;你肯定会觉得作者在忽悠人吧&#xff0c;200 行 C 代码居然可以实现插件式 NOSQL 存储服务器&#xff1f;是的&#xff0c;200 行 C 代码确实可以做到…

bean:header

&#xff08;一&#xff09; 用查询HTTP请求的header信息 header标签用于将特定的请求头信息包装成脚本可以访问的变量。header的用法如下:<bean:header id"variableName" name"headerName"I>&#xff0c; 将 headerName的请求头定义成 variableNam…

jquery validate.addMethod 正则表达式

$(document).ready(function () { /* 设置默认属性 */ $.validator.setDefaults( { submitHandler: function (form) { form.submit(); } } ); // 字符验证 jQuery.validator.addMethod( " stringCheck " , function…

【转帖】Git学习笔记 记录一下

本文内容参考了廖雪峰老师的博文&#xff0c;并做了适当整理&#xff0c;方便大家查阅。 原帖地址 https://wangfanggang.com/Git/git/ 常用命令 仓库初始化 - git init 1 git init 我们新建一个文本文件readme.txt 12 Git is a distributed version control system.Git is f…

转 OFBIZ webservice简介

OFBIZ webservice简介 Opentaps(OFBiz 9.04之后)中webservice用的是AXIS2&#xff0c;最开始自己在网上搜了好多资料&#xff0c;自己拿回来测试&#xff0c;发现都不对。后自己再找了下AXIS的资料说&#xff0c;那种报错很有可能是由于两个版本不对引起的&#xff0c;所以就决…

bean:parameter

bean:parameter查询HTTP请求参数 用于取回请求中的参数值。如果没有指定multiple属性则依据刚取回的值创建一个String类型的bean。如果指定了multiple属性则依据刚取回的值创建一个String[]类型的数组。然后用id属性值将String或String[]绑定到page作用域中(这种绑定是为了其它…

bean:include

bean:include标签不同于JSP里的<jsp:include>之处是此把把包含内容放在一个变量&#xff0c;用时取出 对指定url(由forward、href或page确定)处的资源做一个请求&#xff0c;将响应数据作为一个String类型的bean绑定到page作用域&#xff0c;同时创建一个scripting变量。…