How do I create an object from reflection? GetConstructor () and getdeclaredconstructor () difference?

Source: Internet
Author: User



1. Call the Newinstance () method through the class object, for the parameterless construction Method:



Example: String.class.newInstance ()


 
 
 1 public class Solution {
 2 
 3     public static void main(String[] args) throws Exception {
 4 
 5         Solution solution = Solution.class.newInstance();
 6 
 7         Solution solution2 = solution.getClass().newInstance();
 8 
 9         Class solutionClass = Class.forName("Solution");
10         Solution solution3 = (Solution) solutionClass.newInstance();
11 
12         System.out.println(solution instanceof Solution); //true
13         System.out.println(solution2 instanceof Solution); //true
14         System.out.println(solution3 instanceof Solution); //true
15     }
16     
18 }
 

 





2. Use the GetConstructor () or Getdeclaredconstructor () method of the class object to get the constructor (Constructor) object and call its newinstance () method to create the object, which is suitable for both the parameterless and the argument construction method.



For example: String.class.getConstructor (String.class). newinstance ("Hello");





 
 1 public class Solution {
 2 
 3     private String str;
 4     private int num;
 5 
 6     public Solution() {
 7 
 8     }
 9 
10     public Solution(String str, int num) {
11         this.str = str;
12         this.num = num;
13     }
14 
15     public Solution(String str) {
16         this.str = str;
17     }
18 
19     public static void main(String[] args) throws Exception {
20 
21         Class[] classes = new Class[] { String.class, int.class };
22         Solution solution = Solution.class.getConstructor(classes).newInstance("hello1", 10);
23         System.out.println(solution.str); // hello1
24 
25         Solution solution2 = solution.getClass().getDeclaredConstructor(String.class).newInstance("hello2");
26         System.out.println(solution2.str); // hello2
27 
28         Solution solution3 = (Solution) Class.forName("Solution").getConstructor().newInstance(); // 无参也可用getConstructor()
29         System.out.println(solution3 instanceof Solution); // true
30     }
31 
32 }











difference between GetConstructor () and Getdeclaredconstructor (): *********


Getdeclaredconstructor (class<?> parametertypes)
This method returns all constructors that make parameter types, both public and non- public, and of course private.
The return result of Getdeclaredconstructors () has no filter for the parameter type.

come and see GetConstructor (CLASS<?> parametertypes)
This method returns a subset of the results returned by the previous method, returning only the constructors that make the parameter type access permissions public.
The return result of GetConstructors () also has no filtering of parameter types.





How do I create an object from reflection? GetConstructor () and getdeclaredconstructor () difference?


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.