Java Reflection method invocation and filed settings __java

Source: Internet
Author: User
Tags reflection

Reflection is an advanced feature of Java.

Reflection is a key feature of Java as a dynamic (or quasi dynamic) language. This mechanism allows the program to obtain internal information, including its modifiers (such as public, static, and so on), superclass (for example, object) of any class that has a known name through the reflection APIs at run time, Implementation interfaces (such as Serializable), also includes all information about fields and methods, and can change fields content or Invoke methods at run time.

Reflection is a path to an application that can check itself and run the environment. Alternative-metadata of their own information.

Entry level first see if you call a method by reflection and set the value of a member variable:

1. Set Variable values by reflection

Import Java.lang.reflect.Field; /** * Reflection Modify field * * @author Yblin/public class Reffield {public String color; Main (String args[]) {try {Class c = reffield.class; Field Colorfield; Colorfield = C.getfield ("color"); Field Numberfield = C.getfield ("number"); Reffield obj = new Reffield (); Colorfield.set (obj, "blue"); System.out.println ("color=" + colorfield.get (obj)); Numberfield.set (obj, 2.1); System.out.println ("number=" + numberfield.get (obj)); catch (SecurityException e) {//TODO auto-generated catch block E.printstacktrace ();} catch (Nosuchfieldexception e) { TODO auto-generated Catch block E.printstacktrace (); catch (IllegalArgumentException e) {//TODO auto-generated catch block E.printstacktrace ();} catch (Illegalaccessexcep tion e) {//TODO auto-generated catch block E.printstacktrace ();}}

2. Invoke Reflection Invocation method

Import java.lang.reflect.InvocationTargetException; Import Java.lang.reflect.Method; /** * Reflection method invocation. * * @author Yblin */public class Refmethod {private String color, public void Printcolor () {System.out.println ("Color I S: "+ color);} Public Refmethod (string color) {This.color = color;} public void SayHello (string name, Integer age) {System.out.println ("Hello World," + name + "!" + "The" "The" the ":" + Age);} public static void Main (String args[]) {Class CLS = Refmethod.class; Refmethod ref = new Refmethod ("Blue"); try {method SayHello = cls. GetMethod ("SayHello", new class[] {string.class, integer.class});//Find S with String and Integer parameters Ayhello method. Method Printcolor = Cls.getmethod ("Printcolor", null); try {Sayhello.invoke (ref, new object[] {"Ant", 3})//Invokes method Printcolor.invoke (ref, NULL) by invoking calls to the Mechod object;} catch (Il Legalargumentexception e) {//invalid parameter//TODO auto-generated catch block E.printstacktrace ();} catch (Illegalaccessexception e) {//unauthorized access//TODO auto-generated catch block E.printstacktrace (); catch (InvocationTargetException e) {//TODO auto-generated catch block E.printstacktrace ();}} catch (SecurityException e) {//TODO auto-generated catch block E.printstacktrace ();} catch (Nosuchmethodexception e) { TODO auto-generated Catch block E.printstacktrace (); } } }

 

Related Article

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.