Java basics: Reflection

Source: Internet
Author: User

Reflection is to map each component in the Java class into a java object. Using Reflection technology, you can dissect a class and map each component into objects. The biggest use of reflection is in framework learning.

 

How to obtain the class object corresponding to a class file:

1) class name. class

2) object. getClass ()

3) Class. forName ("Class Name") the Class name must use the fully qualified name of the Class.

// Class name. classClass c1 = String. class; // object. getClass () Class c2 = new String (). getClass (); // Class. forName () Class c3 = Class. forName ("java. lang. string ");

Constructor class:

The instance object of this class represents a constructor of the class.

Use the Constructor class to instantiate an object. The demo is as follows:

 

(1) web. java. reflect. Person class

package web.java.reflect;public class Person {private String name;private Person(){}private Person(String name) {super();this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}public void show(){System.out.println(this.getName()+"is a person");}}

(2) instantiate an object Using Reflection

Package web. java. reflect; import java. lang. reflect. constructor; public class Demo2 {public static void main (String [] args) throws Exception {Class clazz = Class. forName ("web. java. reflect. person "); // obtain the non-argument Constructor c = clazz. getDeclaredConstructor (null); // sets the access of non-public members. setAccessible (true); // create an instance object of this class and convert it to Person p = (Person) c. newInstance (null); // Method p in the execution class. show ();}}

In addition, for non-private constructor methods, you can use getConstructors () to obtain all or getConstructor (***) to obtain the number of specified parameters.

 

Field class:

It represents a member variable in a class.

Package web. java. reflect; import java. lang. reflect. field; public class Demo3 {public static void main (String [] args) throws Exception {Person p = new Person (); // The premise is: the preceding construction method without parameters is default Class c = Class. forName ("web. java. reflect. person "); // obtain the specified private Field f = c. getDeclaredField ("name"); f. setAccessible (true); f. set (p, "Tom"); // equivalent to p. setName ("Tom"); System. out. println (p. getName ());}}

Method class:

It represents a member method in a class.

public class Demo4 {public static void main(String[] args) throws Exception {Class c = Class.forName("cn.itcast.java.reflect.Person");Constructor con = c.getConstructor(null);Person p = (Person) con.newInstance(null);Method m = c.getDeclaredMethod("goodbye",null);m.setAccessible(true);String returnValue = (String) m.invoke(p,null);System.out.println(returnValue);//Method m = c.getMethod("show",String[].class,double.class);//m.invoke(p,new String[]{"sing","dance"},5000);//new Person().show()}}

 

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.