(Reproduced) The parameter version and the parameter-free version of newInstance() are explained in detail lkn

 

Reprinted from: http://xiaohuafyle.iteye.com/blog/1607258 Author: xiaohuafyle

 

There are two ways to create a new class instance through reflection: 
Class.newInstance() 
Constructor.newInstance() 

The following two calling methods are compared and explained: 
Class.newInstance() can only call the constructor without parameters, that is, the default Constructor; 
Constructor.newInstance() can call any constructor according to the parameters passed in. 

Class.newInstance() throws all exceptions thrown by the called constructor. 

Class.newInstance() requires the called constructor to be visible, that is, it must be of public type; 
Constructor.newInstance() can call private constructors in certain circumstances. 

Class A (the called example): 

Java code   Favorite code
  1. public class A {  
  2.     private A() {  
  3.         System.out.println("A's constructor is called.");  
  4.     }  
  5.   
  6.     private A(int a, int b) {  
  7.         System.out.println("a:" + a + " b:" + b);  
  8.     }  
  9. }  


Class B (caller): 

Java code   Favorite code
  1. public class B {   
  2.     public static void main(String[] args) {   
  3.         B b=new B();   
  4.         out.println( "Call private constructor via Class.NewInstance():" );   
  5.         b.newInstanceByClassNewInstance();   
  6.         out.println( "Call private constructor via Constructor.newInstance():" );   
  7.         b.newInstanceByConstructorNewInstance();   
  8.     }   
  9.     /*Create a new class instance via Class.NewInstance()*/   
  10.     private void newInstanceByClassNewInstance(){   
  11.         try {/*当前包名为reflect,必须使用全路径*/   
  12.             A a=(A)Class.forName("reflect.A").newInstance();   
  13.         } catch (Exception e) {   
  14.             out.println("通过Class.NewInstance()调用私有构造函数【失败】");   
  15.         }  
  16.     }  
  17.       
  18.     /*通过Constructor.newInstance()创建新的类示例*/   
  19.     private void newInstanceByConstructorNewInstance(){   
  20.         try {/*可以使用相对路径,同一个包中可以不用带包路径*/   
  21.             Class c=Class.forName("A");   
  22.             /*以下调用无参的、私有构造函数*/   
  23.             Constructor c0=c.getDeclaredConstructor();   
  24.             c0.setAccessible(true);   
  25.             A a0=(A)c0.newInstance();   
  26.             /*以下调用带参的、私有构造函数*/   
  27.             Constructor c1=c.getDeclaredConstructor(new Class[]{int.class,int.class});   
  28.             c1.setAccessible(true);   
  29.             A a1=(A)c1.newInstance(new Object[]{5,6});   
  30.         } catch (Exception e) {   
  31.             e.printStackTrace();   
  32.         }   
  33.     }   
  34. }  


The input results are as follows: 
Call the private constructor 
via Class.NewInstance(): Call the private constructor via Class.NewInstance() [Failure] 
Call the private constructor via Constructor.newInstance(): 
A's constructor is called. 
a:5 b:6 

It means that the method newInstanceByClassNewInstance fails to be called, but the method newInstanceByConstructorNewInstance is called successfully. 
If the constructor of the called class is the default constructor, it is a better choice to use Class.newInstance(), and  a line of
code is OK; if the common people call the called class parameterized constructor and private constructor, 
then Constractor.newInstance() needs to be used, and the two cases depend on the usage. 
However, Constractor.newInstance() is recommended in Java Totorial. 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326679699&siteId=291194637