Java newinstance () parameter versions and no-parameter versions

Source: Internet
Author: User

Newinstance () parameter version and no parameter version Blog Category:
    • Core Java
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
    1. Public class A {
    2. Private A () {
    3. System.out.println ("A ' s constructor is called.");
    4. }
    5. private A (int a, int b) {
    6. System.out.println ("A:" + A + "B:" + B);
    7. }
    8. }

Class B (caller):
Java 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 example via Class.newinstance () */
  10. private void Newinstancebyclassnewinstance () {
  11. try {/* Current package name reflect, must use full path * /
  12. A a= (a) Class.forName ("reflect.   A "). newinstance ();
  13. } catch (Exception e) {
  14. Out.println ("failed" by calling the private constructor via Class.newinstance ());
  15. }
  16. }
  17. /* Create a new class example via Constructor.newinstance () */
  18. private void Newinstancebyconstructornewinstance () {
  19. try {/* can use a relative path, which can be used without a package path in the same package * /
  20. Class c=class.forname ("A");
  21. /* Call the non-parameter, private constructor */
  22. Constructor C0=c.getdeclaredconstructor ();
  23. C0.setaccessible (true);
  24. A a0= (a) c0.newinstance ();
  25. /* Call the parameter, private constructor */
  26. Constructor C1=c.getdeclaredconstructor (new class[]{Int.class,int.class});
  27. C1.setaccessible (true);
  28. A a1= (a) c1.newinstance (new object[]{5,6});
  29. } catch (Exception e) {
  30. E.printstacktrace ();
  31. }
  32. }
  33. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.