Java Reflection Call Method

Source: Internet
Author: User

Methods in the 1:class class

Getdeclaredmethod (String name,                                Class<?> parametertypes)                         throws Nosuchmethodexception,                                SecurityException
Number of references:
name -Method name
parameterTypes -array of parameters
Return:
The object of the method that matches the specified name and the number of parameters of the class Method

Methods in the 2:method class;

Invoke (Object obj,                     Object ... args)              throws Illegalaccessexception,                     illegalargumentexception,                     invocationtargetexception
Number of references:
obj -Object from which the underlying method is called
args -The number of parameters used for the method call
Return:
Use a number of parameters argsIn objThe result of assigning the method represented by the object 3:hello the reflection call at the world! level:

package Com.dao.text;import Java.lang.reflect.invocationtargetexception;import Java.lang.reflect.method;public Class Tetsreflect {/** * @param args * @throws nosuchmethodexception * @throws SecurityException * @throws invocationtargetexcep tion * @throws illegalaccessexception * @throws illegalargumentexception */public static void Main (string[] args) throws S Ecurityexception,nosuchmethodexception, Illegalargumentexception,illegalaccessexception, invocationtargetexception {Person p = new person (); Class CLA = P.getclass ();//1: No reference method method1 = Cla.getdeclaredmethod ("print"), Method1.invoke (P);//2: Call method with a number of parameters METHOD2 = Cla.getdeclaredmethod ("Printparameter", String.class); Method2.invoke (P, "Hello world!!");}} class Person {public void print () {System.out.println ("Hello world!!" + "Call without parameter Reflection method");} public void Printparameter (String param) {System.out.println (param+ "Call to the reflection method with a reference");}} 

Results:

Hello world!! Call without a parameter reflection method
Hello world!! Invocation of a reflection method with a number of parameters

Java Reflection Call Method

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.