java 反射创建实例与new创建实例的区别
new创建实例
new创建一个编译时已知的类的实例,也即是静态的创建实例;
可以调用类的任何构造器来创建实例;
速度更快,由于可以将需要的类写入字节文件中(hardcoded into the bytecode)。
反射创建实例(如Class.forName().newInstance())
反射创建实例是动态的创建一个类的实例;
只能调用类的无参数构造器来创建实例;
速度较慢。
为什么需要Class.forName("your class name").newInstance()创建实例
例如需要从远端动态地加载类,只知道这些类的名字,是无法使用import将其导入程序中的,这个时候可以利用反射创建实例
(以上内容整理自StackOverFlow)
The new operator creates a new object of a type that's known statically (at compile-time) and can call any constructor on the object you're trying to create. It's the preferred way of creating an object - it's fast and the JVM does lots of aggressive optimizations on it.
Class.forName().newInstance() is a dynamic construct that looks up a class with a specific name. It's slower than using new because the type of object can't be hardcoded into the bytecode, and because the JVM might have to do permissions checking to ensure that you have the authority to create an object. It's also partially unsafe because it always uses a zero-argument constructor, and if the object you're trying to create doesn't have a nullary constructor it throws an exception.
In short, use new if you know at compile-time what the type of the object is that you want to create. Use Class.forName().newInstance() if you don't know what type of object you'll be making.
Class.forName("your class name").newInstance() is useful if you need to instantiate classes dynamically, because you don't have to hard code the class name to create an object.
Imagine a scenario where you load classes dynamically from a remote source. You will know their names but can't import them at compile time. In this case you can't use new to create new instances. That's (one reason) why Java offers the newInstance() method.
Class.forName("your class name").newInstance() is used when you want to get an instance form a class work similar to new, but it is useful when you want to get instance from a class in a jar file or remote server and you can not import it in compile time.
ex: Class.forName("YOUR JDBC DRIVER").newInstance(), you can not import the JDBC class at compile time.
java 反射创建实例与new创建实例的区别的更多相关文章
- Java反射04 : 通过Array动态创建和访问Java数组
java.lang.reflect.Array类提供了通过静态方法来动态创建和访问Java数组的操作. 本文转载自:https://blog.csdn.net/hanchao5272/article/ ...
- 一个例子让你了解Java反射机制
本文来自:blog.csdn.net/ljphhj JAVA反射机制: 通俗地说,反射机制就是可以把一个类,类的成员(函数,属性),当成一个对象来操作,希望读者能理解,也就是说,类,类的成员,我们在运 ...
- (转)个例子让你了解Java反射机制
个例子让你了解Java反射机制 原文地址:http://blog.csdn.net/ljphhj/article/details/12858767 JAVA反射机制: 通俗地说,反射机制就是可 ...
- (转)JAVA反射机制理解
JAVA反射机制: 通俗地说,反射机制就是可以把一个类,类的成员(函数,属性),当成一个对象来操作,希望读者能理解,也就是说,类,类的成员,我们在运行的时候还可以动态地去操作他们. 理论的东东太多也没 ...
- java 反射(Reflection)
看了很多关于java 反射的文章,自己把所看到的总结一下.对自己,对他人或多或少有帮助吧. Java Reflection是什么? 首先来看看官方文档Oracle里面对Reflection的描述: R ...
- 【java提高】---java反射机制
java反射机制 一.概述 1.什么是反射机制 反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态 ...
- Java反射-初步入门
Java反射-初步入门 学反射先了解什么是反射. 百度百科:JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动 ...
- Java 23种设计模式详尽分析与实例解析之一--创建型模式
面向对象的设计原则 常用的面向对象设计原则包括7个,这些原则并不是独立存在的,它们相互依赖.互为补充. Java设计模式 创建型模式 简单工厂模式 模式动机: 考虑一个简单的软件应用场景,一个软件系统 ...
- java反射 实例
首先介绍几个概念: 1.Java反射的概念 反射含义:可以获取正在运行的Java对象. 2.Java反射的功能 1)可以判断运行时对象所属的类 2)可以判断运行时对象所具有的成员变量和方法 3)通过反 ...
- Java反射机制(创建Class对象的三种方式)
1:SUN提供的反射机制的类: java.lang.Class<T> java.lang.reflect.Constructor<T> java.lang.reflect.Fi ...
随机推荐
- L323 英语有必要学语法吗
The Agony and Ecstasy of Grammar “Underline a relative clause.” This challenge would give a lot of a ...
- L312 难看懂的
There are few sadder sights than 8 pile of fan letters ,lovingly decorated with hand drawings,suffer ...
- Oracle使用exp和imp导出、导入数据
===========导出============ exp 用户名/密码@服务器(localhost) file=文件路径.dmp owner=(用户名) ===========导入========= ...
- 配置MAVEN环境变量
在配置maven前一定要先配置java jdk 的环境变量 和java配置一样 先写一个MAVEN_HOME:F:\java\apache-maven-3.5.2 (就是你maven 的下载位置) 写 ...
- Kafka的安装 -- 未完成
1. 官网下载软件 2. linux服务器上, 安装上传和下载的工具 yum install -y lrzsz rz : 上传 sz + 文件名 : 下载 3.解压文件 pwd: 查看当前路径 解压到 ...
- Javascript将数据转成英文书写格式
var aTens = [ "TWENTY", "THIRTY", "FORTY", "FIFTY", "SI ...
- HDU 6059 17多校3 Kanade's trio(字典树)
Problem Description Give you an array A[1..n],you need to calculate how many tuples (i,j,k) satisfy ...
- LazyMan的实现
题目如下: 实现一个LazyMan,可以按照以下方式调用: LazyMan(“Hank”)输出: Hi! This is Hank! LazyMan(“Hank”).sleep(10).eat(“di ...
- CompletableFuture
若你的意图是并发,而非并行,或者你的主要目标是在同一个CPU上执行几个松耦合的任务,充分利用CPU的核,让其足够忙碌,从而最大化程序的吞吐量,那么其实真正想做的避免因为等待远程服务的返回,或对数据库的 ...
- Linux 双网卡双网段通信
/********************************************************************************* * Linux 双网卡双网段通信 ...