The reflection of Java

Source: Internet
Author: User

1: Loading of classes
When we use a class for the first time, the system does two things:
1: Load the class's bytecode file (. class) file into memory
2: The system also creates an object for the bytecode file: Class object
A.txt Hello.java d://lesson//a.txt---->file class
A.class hello.class demo.class---->class class

2: When a class is loaded
1. Creating an instance of a class
2. Static variables for a class, or assign a value to a static variable
3. Static methods for classes
4. Use reflection to force the creation of a Java.lang.Class object for a class or interface
5. Initialize subclasses of a class
6. Use the Java.exe command directly to run a main class


3: The effect of the reflection
1: Reflection allows us to compile with a non-existent or later to exist class
2: Reflection can access private members of a class MD5 algorithm
3: Through reflection, we write some common code

4: Steps of Reflection
1: Get Class object//A class has only one class object in memory
2: Gets the members of the class through the class object (constructor, member variable, member method)
3: Access to these variables and methods


Words:
Class
? Constructor Construction
? Method methods
? Field fields
? Instance instances
? Invoke execution


5: Get the members of the class through class
1: Get the constructor method for a class
A: Get all the constructs
Public constructor<?>[] GetConstructors ()//can only get public decorated constructs
Public constructor<?>[] Getdeclaredconstructors ()//can get all the constructs (including private)
B: Get a single construct
Public constructor<t> getconstructor (class<?> ... parametertypes)
Public constructor<t> getdeclaredconstructor (class<?> ... parametertypes)
2: Get Member method
A: Get all the member methods
Public method[] GetMethods ()
Public method[] Getdeclaredmethods ()
B: Get a single member method
Public Method GetMethod (String name, Class<?> .... parametertypes)
Public Method Getdeclaredmethod (String name, Class<?> .... parametertypes)

4: Get member variables
A: Get all the member variables
Public field[] GetFields ()
Public field[] Getdeclaredfields ()
B: Get a single member variable
Public Field GetField (String name)
Public Field Getdeclaredfield (String name)

The reflection 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.