"Java" Method.invoke (object/null,new object[]{actual parameter) (method bottom)

Source: Internet
Author: User

Both static/non-static, fixed/variable parameters are wrapped in an object array for the reflection method.

1  PackageCom.tn.clas;2 3 ImportJava.lang.reflect.Method;4 Importjava.util.Arrays;5 6  Public classClient {7      Public Static voidMain (string[] args)throwsException {8Class<user> Clas=user.class;9Method M=clas.getmethod ("Method", string[].class);TenM.invoke (NULL,Newobject[]{Newstring[]{"AA", "BB", "CC"}});//static methods can omit objects, replace them directly with NULL, or use Clas One          AM=clas.getdeclaredmethod ("Method",int[].class);//non-public methods are obtained using declared -M.setaccessible (true);//the non-public method needs to be set to accessible -M.invoke (Clas.newinstance (),New int[]{1,2,3,4,3,2,1}];//non-static methods need to provide the underlying class object the     } - } -  - classuser{ +      Public Static voidMethod (string...strings) { - System.out.println (Arrays.tostring (strings)); +     } A      at     Private voidMethodint... ints) { - System.out.println (arrays.tostring (ints)); -     } -}

"Java" Method.invoke (object/null,new object[]{actual parameter) (method bottom)

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.