The principle of the reflection mechanism of Java

Source: Internet
Author: User

The concept of a reflection mechanism:

In the running state, for any class, you can know all the properties and methods of the class, and for any one object, you can call any of its methods. This dynamic access to information, and the ability to dynamically invoke object methods, is called the reflection mechanism of the Java language.

Application of two-reflection mechanism:

Generate dynamic proxies for slicing programming (before and after calling method Katan).

The principle of three-reflection mechanism:

1 First Clear concept: Everything is Object----class is also an object.

2 then know the contents of the class: Modifier constructor field method.

3 Second understand load: When animal.class in the hard disk, is a file, when loaded into memory, can be considered an object, is the object of Java.lang.class.

When you run a program, the Animal.class object is generated when instantiated, what is the difference between the two objects?

For example, the Animal.class object above is an object of Java.lang.class, and the following object is the object produced by the Animal.class instantiation

4 when Animal.class is loaded into the JVM, how does the JVM identify the methods, member variables, construction methods, etc. contained in the Animal.class?

For example, animal.class the member variable in the object's color = red;

When loaded into memory, the fur color is =null, and the color is assigned red when it is instantiated.

Animal.class Color This property is a property value for Java.lang.class, which is also an object for java.lang.class. This is also the reason that the reflection mechanism dynamically acquires information and dynamically invokes object methods.

It's a method in Java.lang.class.

In Java.lang.class, the color of a field is a property value, animal all methods, member variables, constructors, modifiers are written in accordance with the standards of java.lang.class, or will be compiled when the error

Four-ray mechanism simple example code

public class _14_reflecttest07

{

public static void Main (string[] args) throws exception{

1. Get the class

Class C = class.forname ("_12_customerservice");

Get a specific method

By: Method name + formal parameter list

Method m = C.getdeclaredmethod ("login", String.class,string.class);

The login method is executed through the reflection mechanism.

Object o = c.newinstance ();

The M method of the O object is called, passing "admin" "123" parameter, the result of the method is RetValue

Object RetValue = M.invoke (o, "admin", "123");

System.out.println (RetValue); True

}

}

public class _12_customerservice

{

Login

public boolean login (String name,string pwd) {

if ("admin". Equals (name) && "123". Equals (pwd)) {

return true;

}

return false;

}

Exit

public void logout () {

SYSTEM.OUT.PRINTLN ("The system has safely exited! ");

}

}

The principle of the reflection mechanism 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.