当前位置:网站首页>创建对象之 new 和 newInstance() 的区别
创建对象之 new 和 newInstance() 的区别
2022-06-09 02:33:00 【Ray327_】
Person p1 = new Person();
Person p2 = new Person("帅哥");new 是关键字,我们可以任意调用构造函数来创建对象实例;若对应类的class文件未加载,则加载对应的class文件,进行类的链接、初始化操作。
在调用空参构造器的时候new和newInstance() 的效果是一样的
//Class clazz = Person.class;
Class clazz = Class.forName("com.ray.java.Person");
//创建运行时类的对象
Person p3 = (Person) clazz.newInstance();newInstance() 是方法,返回Object类型,创建对应的运行时类的对象。内部调用了运行时类的空参的构造器。
要想此方法正常的创建运行时类的对象,要求:
运行时类必须提供空参的构造器
空参的构造器的访问权限得够。通常,设置为
public。在
javabean中要求提供一个public的空参构造器。原因:
便于通过反射,创建运行时类的对象
便于子类继承此运行时类时,默认调用
super()时,保证父类此构造器。
从JVM的角度看:
我们使用关键字new创建一个类的时候,这个类可以没有被加载。但是使用newInstance()方法的时候,就必须保证:
1.这个类已经加载
2.这个类已经连接了
而完成上面两个步骤的正是Class的静态方法forName()所完成的,这个静态方法调用了启动类加载器,即加载 java API的那个加载器。
现在可以看出,newInstance()实际上是把new这个方式分解为两步,即首先调用Class加载方法加载某个类,然后实例化。 这样分步的好处是显而易见的。我们可以在调用class的静态加载方法forName时获得更好的灵活性,提供给了一种降耦的手段。
在初始化一个类,生成一个实例的时候,new关键字和newInstance()方法除了一个是关键字,一个是方法外,最主要有什么区别?它们的区别在于创建对象的方式不一样,前者是创建一个新类,后者是使用类加载机制。那么为什么会有两种创建对象方式?这主要考虑到软件的可伸缩、可扩展和可重用等软件设计思想。
Java中工厂模式经常使用newInstance来创建对象,因此从为什么要使用工厂模式上也可以找到具体答案。
例如:
Class clazz = Class.forName("A");
InterfaceA factory = (InterfaceA)clazz.newInstance();
//其中InterfaceA是A的接口进一步的:
String className = "A";
Class clazz = Class.forName(className);
InterfaceA factory = (InterfaceA)clazz.newInstance();进两步的:
String className = "用户输入的或配置文件读取的字符串";
Class clazz = Class.forName(className);
InterfaceA factory = (InterfaceA)clazz.newInstance();上面代码已经不存在固定的类名称,它的优点是,无论className类怎么变化,上述代码不变,还可以传入其他的实现了InterfaceA接口的实现类。newInstance: 弱类型。低效率。只能调用无参构造。
由此可见newInstance()是实现IOC、反射、面对接口编程 和 依赖倒置 等技术方法的必然选择,new 只能实现具体类的实例化,不适合于接口编程。
边栏推荐
- Blue Bridge Cup_ N-ary linear equations_ Gauss elimination
- [wustctf 2020] plain
- Should flying books be ecological? Analyze the first independent SaaS case of all in flybook
- 杰理之SPI 主机如何配置驱动程序?【篇】
- 在业务代码中使用redis实现缓存效果
- [coding streaming] installation and use of SRS streaming media server
- How to modify ad_ Key connected pin? [chapter]
- Doris daily function summary I
- Read the log + regularize and extract the desired content + write the script to csv/xlsx
- 得物技术埋点自动化验证的探索和最佳实践
猜你喜欢

Embracing out of hospital prescription drugs, Internet medicine should also "get rid of virtual reality"?

Navicat tool batch imports JSON format data to Doris

技术负责人如何搞垮一个团队?

20. Class E power amplifier design for ads usage record (Part 1)

21、ADS使用记录之E类功放设计(中)

Should flying books be ecological? Analyze the first independent SaaS case of all in flybook

Redis cluster setup

FFmpeg的软、硬解码方式梳理

toggleRowSelection()失效的2个重要影响因素

Jedis tool class, adapting to a single redis and redis cluster
随机推荐
SQLite3 syntax (2)
String 3-387. First unique character in string
编写循环何条件任务
Integrated base process test summary
pkg-config --modversion opencvPackage opencv was not found in the pkg-config search path. Perhaps y
Common commands for detecting Huawei network devices
(10.3)【隐写缓解】隐写防护、隐写干扰、隐写检测
Live short video app development
Kubernetes scheduling framework extension point
S series · add data to the text file without adding duplicate values
杰理之SPI 主机如何配置驱动程序?【篇】
Classify the audio and put it into the corresponding folder according to the file name
Deux facteurs importants affectant la défaillance de togglerowselection ()
GeoTrust certificate price
pkg-config --modversion opencvPackage opencv was not found in the pkg-config search path.Perhaps y
FRP construction
Knowledge points of 2022 information security engineer examination: configuration and use of network security products
Jedis工具类、适配单个redis以及redis集群
蓝桥杯_青蛙的约会_扩展欧几里得
vins feature_ track