Java reflection: Creating instances

Source: Internet
Author: User

To makeProgramIt has higher flexibility and configurability. during development, we often use reflection to create a class instance, that is, to obtain an object of a class! The common practice is to use the class. newinstance is obtained. But this method has a bad point, that is, it can only create an object without a parameter constructor. If the constructor has a parameter, it cannot do anything; the solution is to first obtain the constructor with parameters through the class of this class, and then create an instance through the constructor and the initialization parameters for passing the example! The following is a demo of the above practice:

 

 

Entity class:

  1. PackageReflect;
  2. Public ClassDuck {
  3. PrivateInteger weight;
  4. PrivateString name;
  5. PublicDuck (){
  6. System. Out. println ("No parameter is called");
  7. }
  8. PublicDuck (string name ){
  9. Name = Name;
  10. System. Out. println ("One Parameter called");
  11. }
  12. PublicDuck (string name, integer weight ){
  13. Name = Name;
  14. Weight = weight;
  15. System. Out. println ("Two Parameter called");
  16. }
  17. }

  1. PackageReflect;
  2. ImportJava. Lang. Reflect. constructor;
  3. ImportJava. Lang. Reflect. invocationtargetexception;
  4. Public ClassExetest {
  5. Public Static VoidMain (string [] ARGs ){
  6. <Span style ="Color: #000000"> Class CLS = duck.Class; </Span>
  7. Class conargs [] = {string.Class, Integer.Class};
  8. Try{
  9. <Span style ="Color: #000000"> Constructor = Cls. getconstructor (conargs); </span>
  10. String name ="ABC";
  11. Integer Weight =NewINTEGER (1056);
  12. Object initargs [] = {name, weight };
  13. <Span style ="Color: #000000"> Constructor. newinstance (initargs); </span>
  14. }Catch(Securityexception e ){
  15. E. printstacktrace ();
  16. }Catch(Nosuchmethodexception e ){
  17. E. printstacktrace ();
  18. }Catch(Illegalargumentexception e ){
  19. // Todo auto-generated Catch Block
  20. E. printstacktrace ();
  21. }Catch(Instantiationexception e ){
  22. // Todo auto-generated Catch Block
  23. E. printstacktrace ();
  24. }Catch(Illegalaccessexception e ){
  25. // Todo auto-generated Catch Block
  26. E. printstacktrace ();
  27. }Catch(Invocationtargetexception e ){
  28. // Todo auto-generated Catch Block
  29. E. printstacktrace ();
  30. }
  31. }
  32. }

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.