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创建实例的区别的更多相关文章

  1. Java反射04 : 通过Array动态创建和访问Java数组

    java.lang.reflect.Array类提供了通过静态方法来动态创建和访问Java数组的操作. 本文转载自:https://blog.csdn.net/hanchao5272/article/ ...

  2. 一个例子让你了解Java反射机制

    本文来自:blog.csdn.net/ljphhj JAVA反射机制: 通俗地说,反射机制就是可以把一个类,类的成员(函数,属性),当成一个对象来操作,希望读者能理解,也就是说,类,类的成员,我们在运 ...

  3. (转)个例子让你了解Java反射机制

    个例子让你了解Java反射机制   原文地址:http://blog.csdn.net/ljphhj/article/details/12858767 JAVA反射机制:   通俗地说,反射机制就是可 ...

  4. (转)JAVA反射机制理解

    JAVA反射机制: 通俗地说,反射机制就是可以把一个类,类的成员(函数,属性),当成一个对象来操作,希望读者能理解,也就是说,类,类的成员,我们在运行的时候还可以动态地去操作他们. 理论的东东太多也没 ...

  5. java 反射(Reflection)

    看了很多关于java 反射的文章,自己把所看到的总结一下.对自己,对他人或多或少有帮助吧. Java Reflection是什么? 首先来看看官方文档Oracle里面对Reflection的描述: R ...

  6. 【java提高】---java反射机制

    java反射机制 一.概述 1.什么是反射机制 反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态 ...

  7. Java反射-初步入门

    Java反射-初步入门 学反射先了解什么是反射. 百度百科:JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动 ...

  8. Java 23种设计模式详尽分析与实例解析之一--创建型模式

    面向对象的设计原则 常用的面向对象设计原则包括7个,这些原则并不是独立存在的,它们相互依赖.互为补充. Java设计模式 创建型模式 简单工厂模式 模式动机: 考虑一个简单的软件应用场景,一个软件系统 ...

  9. java反射 实例

    首先介绍几个概念: 1.Java反射的概念 反射含义:可以获取正在运行的Java对象. 2.Java反射的功能 1)可以判断运行时对象所属的类 2)可以判断运行时对象所具有的成员变量和方法 3)通过反 ...

  10. Java反射机制(创建Class对象的三种方式)

    1:SUN提供的反射机制的类: java.lang.Class<T> java.lang.reflect.Constructor<T> java.lang.reflect.Fi ...

随机推荐

  1. golang fmt占位符

    golang fmt格式"占位符" golang 的fmt 包实现了格式化I/O函数,类似于C的 printf 和 scanf. 定义示例类型和变量 type Human stru ...

  2. MYSQL escape用法--转义

    在sql like语句中,比如 select * from user where username like '%nihao%',select * from user where username l ...

  3. ubuntu + samba 共享失败

    主机版本:ubuntu14.04 问题描述:Samba服务配置后仍然不通. samba服务这么成熟的东西,本以为在ubuntu上修改下smb.conf重启就得了,但是访问还是失败. 表现是,可以看到s ...

  4. java pojo类

    POJO POJO是Plain OrdinaryJava Object的缩写 可以当作简单的Java对象 实际就是普通JavaBeans 外文名 POJO 实际意义 普通JavaBeans 全     ...

  5. 计时器---JS

    <script type="text/javascript"> function count(){ var a=document.getElementById(&quo ...

  6. APK模式下,epg版本升级,需要做同步

    采用安卓盒子(APK模式),需要在管理节点做一个同步: 同步目录: /var/www/html/upgradestb 从管理节点向三台业务节点同步: 2018/01/19 conf/server.co ...

  7. matlab数组和矩阵

    数组创建 要创建每行包含四个元素的数组,请使用逗号 (,) 或空格分隔各元素. a = [1 2 3 4] a = 1×4 1 2 3 4 这种数组为行向量. 要创建包含多行的矩阵,请使用分号分隔各行 ...

  8. root/base/stem概念

    The verb root (or base form): 1.is the same as the infinitive不定式 (e.g., to dive, to jump, to wonder) ...

  9. PWA需要的技术

    1 Manifest                      https://developer.mozilla.org/zh-CN/docs/Web/Manifest 2 Service Work ...

  10. HDU 1711:Number Sequence(KMP模板,求位置)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...