Invoking methods in a class using the reflection function of Java

Source: Internet
Author: User

Recently, we have been doing the reflection of Java, there are a lot of problems with the method of reflection calling today, the main point is that there is no detailed reference to the official API. So go a lot of detours.

So I want to write down this example so that I can learn from others.

Package Com.mine.practice.reflect;import Java.lang.reflect.invocationtargetexception;import  java.lang.reflect.method;/** * method in call class using reflection * @author 2014-11-5 a.m. 10:51:28 * @version V1.0 * @modify by User: {Modified by}       2014-11-5 * @modify by reason:{method name}:{Reason} */public class user{/** user name */String name; @SuppressWarnings ("unused") public static void main (string[] args) throws SecurityException, Nosuchmethodexception, Ill  Egalargumentexception, Illegalaccessexception, invocationtargetexception{//1, instantiating the object user user = new user (); 2. The list of parameters required in the method.     A single parameter can also be used directly with the string params = "Tom";          string[] params = new string[]{"Tom"};     3.1 methods that require a parameter//the first parameter is the name of the method, the second argument is the type of the method parameter, and a single argument can be written directly xxx.class or as an array.     Method Setmethod = (user.class). GetMethod ("SetName", New Class[]{string.class});         Call the SetName method, because there is no return value, so you can not use the object to receive, if you receive the returned is also null setmethod.invoke (User,params);     3.2 Methods that do not require parameters//The first parameter is the name of the method, because there are no parameters, so only one parameter is required. Method GetMethod = (user.class). GetmethoD ("GetName");     Call the GetName method because there is a return value, so you can define an object to receive the return value. Note that the class in invoke, which is the following user, must be instantiated, otherwise the following exception will be thrown//java.lang.illegalargumentexception:object is not a instance of declaring CL         The Getmethod.invoke Object object = The user;        Print uses reflection to get the return value of the method System.out.println (object);  } public void SetName (String name) {this.name = name;  } public String GetName () {return name; }}


Invoking methods in a class using the reflection function of Java

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.