Newinstance () parameter version and no parameter version
Blog Category:
There are two ways to create a new class sample from reflection:
Class.newinstance ()
Constructor.newinstance ()
The following two methods of invocation are compared to illustrate:
Class.newinstance () can only invoke the parameterless constructor, which is the default constructor;
Constructor.newinstance () can call any of the constructor constructors based on the parameters passed in.
Class.newinstance () throws all exceptions thrown by the called constructor.
Class.newinstance () requires that the constructor being called is visible, which must be of the public type;
Constructor.newinstance () in a specific case, you can call a private constructor.
Class A (The called example):
Java code
- Public class A {
- Private A () {
- System.out.println ("A ' s constructor is called.");
- }
- private A (int a, int b) {
- System.out.println ("A:" + A + "B:" + B);
- }
- }
Class B (caller):
Java code
- Public class B {
- public static void Main (string[] args) {
- b b=new B ();
- Out.println ("Call private constructor via Class.newinstance ():");
- B.newinstancebyclassnewinstance ();
- Out.println ("Call private constructor via Constructor.newinstance ():");
- B.newinstancebyconstructornewinstance ();
- }
- /* Create a new class example via Class.newinstance () */
- private void Newinstancebyclassnewinstance () {
- try {/* Current package name reflect, must use full path * /
- A a= (a) Class.forName ("reflect. A "). newinstance ();
- } catch (Exception e) {
- Out.println ("failed" by calling the private constructor via Class.newinstance ());
- }
- }
- /* Create a new class example via Constructor.newinstance () */
- private void Newinstancebyconstructornewinstance () {
- try {/* can use a relative path, which can be used without a package path in the same package * /
- Class c=class.forname ("A");
- /* Call the non-parameter, private constructor */
- Constructor C0=c.getdeclaredconstructor ();
- C0.setaccessible (true);
- A a0= (a) c0.newinstance ();
- /* Call the parameter, private constructor */
- Constructor C1=c.getdeclaredconstructor (new class[]{Int.class,int.class});
- C1.setaccessible (true);
- A a1= (a) c1.newinstance (new object[]{5,6});
- } catch (Exception e) {
- E.printstacktrace ();
- }
- }
- }
The input results are as follows:
The private constructor is called through Class.newinstance ():
Calling private constructor "failed" via Class.newinstance ()
The private constructor is called through Constructor.newinstance ():
A ' s constructor is called.
A:5 B:6
The description method newinstancebyclassnewinstance the call failed, and the method newinstancebyconstructornewinstance the call succeeded.
If the constructor of the called class is the default constructor, using Class.newinstance () is the better choice.
A code is OK, if the common people call the called class with a parameter constructor, a private constructor,
It is necessary to use Constractor.newinstance (), both of which are subject to availability.
However, Constractor.newinstance () is recommended in Java totorial.
Java newinstance () parameter versions and no-parameter versions