Home  >  Article  >  Java  >  Introduction to the differences between the newInstance() method and the new keyword in Java

Introduction to the differences between the newInstance() method and the new keyword in Java

黄舟
黄舟Original
2017-09-02 10:33:171499browse

This article mainly introduces relevant information about the difference between the newInstance() method and the new keyword in Java. I hope that through this article you can master the difference and usage of their homes. Friends in need can refer to it

The difference between the newInstance() method and the new keyword in java

* The difference lies in the way of creating objects. The former uses the class loading mechanism, and the latter creates a new object. kind.
* So why are there two ways to create objects? This mainly takes into account software design ideas such as scalability, extensibility and reusability of the software.
* When we use the keyword new to create a class, the class may not be loaded. But when using the newInstance() method,
* must ensure that: 1. This class has been loaded; 2. This class has been connected.
* newInstance() actually decomposes the new method into two steps, that is, first calls the Class loading method to load a certain class, and then instantiates it.
* The benefits of this step-by-step approach are obvious. We can get better flexibility when calling the static loading method forName of the class.
* provides a means of decoupling (lowering the degree of coupling).
* Finally, use the simplest description to distinguish the difference between the new keyword and the newInstance() method:
* newInstance: Weak type. ineffective. Only constructors without parameters can be called.
* new: Strong type. Relatively efficient. Can call any public constructor.

The code is as follows:


import java.lang.reflect.InvocationTargetException; 
import java.lang.reflect.Method; 
import java.lang.reflect.Field; 
 
public class testInvoke { 
  public void work(){ 
    System.out.println("-----------"); 
  }  
  public testInvoke work(String a,Integer b){ 
    System.out.println(a + b); 
    return this; 
  } 
  public void work(Integer b, int c ){ 
    System.out.println(b + c); 
  } 
   
  public static void main(String[] args) throws SecurityException, NoSuchMethodException, InstantiationException,     IllegalAccessException, IllegalArgumentException, InvocationTargetException{ 
    Class<?> clazz = testInvoke.class; 
    //Class<?> clazz = Class.forName("invoke.testInvoke"); 
    //testInvoke tinvoke = new testInvoke(); Class<?> clazz = tinvoke.getClass();  
    System.out.println(clazz); 
    //如果源类的方法没有参数,则要用new Class[]{} 
    Method method1 = clazz.getMethod("work", new Class[]{}); 
    Method method2 = clazz.getMethod("work", new Class[]{String.class, Integer.class});  
    Method method3 = clazz.getMethod("work", new Class[]{Integer.class, int.class}); 
    Object invokeTest = clazz.newInstance(); 
    /* 
     * Method类的invoke(Object obj,Object args[])方法接收的参数必须为对象,<br/> 
     * 如果参数为基本类型数据,必须转换为相应的包装类型的对象。invoke()方法的返回值总是对象,<br/> 
     * 如果实际被调用的方法的返回类型是基本类型数据,那么invoke()方法会把它转换为相应的包装类型的对象,再将其返回<br/> 
     */ 
    //invoke方法的第一个参数是源类的实例,第二个参数是实例的值 
    Object result1 = method1.invoke(invokeTest, new Object[]{}); 
    Object result2 = method2.invoke(invokeTest, new Object[]{"aaaa",new Integer(10)}); 
    Object result3 = method3.invoke(invokeTest, new Object[]{3,new Integer(4)}); 
    System.out.println(result1); 
    System.out.println(result2); 
    System.out.println(result3); 
 
    Method[] methods = clazz.getMethods(); 
    for(Method method : methods){ 
      System.out.println(method.getName()); 
    } 
     
    Field[] fileds = clazz.getFields(); 
    for(Field filed: fileds){ 
      System.out.println(filed.getName()); 
    } 
  } 
}

Console information:


class invoke.testInvoke
-----------
aaaa10
7
null
invoke.testInvoke@de6ced
null
work
[Ljava.lang.Class;@c17164
work
[Ljava.lang.Class;@1fb8ee3
work
[Ljava.lang.Class;@61de33
main
[Ljava.lang.Class;@14318bb
wait
[Ljava.lang.Class;@ca0b6
wait
[Ljava.lang.Class;@10b30a7
wait
[Ljava.lang.Class;@1a758cb
equals
[Ljava.lang.Class;@1b67f74
toString
[Ljava.lang.Class;@69b332
hashCode
[Ljava.lang.Class;@173a10f
getClass
[Ljava.lang.Class;@530daa
notify
[Ljava.lang.Class;@a62fc3
notifyAll
[Ljava.lang.Class;@89ae9e

The above is the detailed content of Introduction to the differences between the newInstance() method and the new keyword in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn