Differences between the newInstance of method and the new keyword in java

  • 2020-09-28 08:52:30
  • OfStack

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

* The difference is that objects are created in a different way, using class loading versus creating a new class.
* So why are there two ways to create objects? This mainly considers the software scalable, extensible and reusable software design ideas.
* When we create a class using the keyword new, the class may not be loaded. But with the newInstance() method,
* must ensure: 1, the class has been loaded; 2. This class is already connected.
* newInstance() actually breaks down the new approach into two steps: first, call the Class load method to load a class, and then instantiate it.
* The benefits of such a step-by-step approach are obvious. We can get more flexibility in calling class's static load method, forName,
* Provides a means of decoupling (to reduce coupling).
* Finally, use the simplest description to distinguish the new keyword from the newInstance() method:
* newInstance: Weak type. Low efficiency. You can only call a parametrically free construct.
* new: Strong type. Relatively efficient. Any public construct can be called.

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); 
    // If the method of the source class has no arguments, use 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 Of the class invoke(Object obj,Object args[]) Method must receive parameters that are objects, <br/> 
     *  If the parameter is base-type data, it must be converted to an object of the corresponding wrapper type. invoke() The return value of a method is always an object, <br/> 
     *  If the return type of the actual method being called is base-type data invoke() Method converts it to an object of the appropriate wrapper type and returns it <br/> 
     */ 
    //invoke Methods the first 1 An instance of the source class 2 The parameters are the values of the instance  
    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

If you have any questions, please leave a message or go to this site community exchange discussion, thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: