The Java self-study road-day21

Source: Internet
Author: User

JAVA21

Loading of class loader classes

When a program is to use a class, if it has not been loaded into memory, the system will initialize the class by loading the connection initialization three steps.

L Load

    1. The class's loader reads the class file into memory, puts it into the method area, and creates a class object for it
    2. is an object of the compiled class file
    3. Object into heap memory
    4. Also called bytecode object
    5. You can use not to create

L Connection

    1. Verify that you have the correct internal structure
    2. Allocating space for static members and setting default initialization values
    3. Parsing converts a symbolic reference in a class's binary data to a direct reference such as a=1 in memory directly to 1 without the symbol a

L Initialization

is to create the object

Load time of Class

L Create a new class with the Main method

L Other classes call method member variables in this class or instantiate create objects

L Other classes call method member variables or instantiate objects in subclasses of this class

Running any class is a way to run the object class first

=

The composition of the loader for the class

L Root Class Loader

    1. Responsible for loading the core class library
    2. Rt.jar in the JRE directory
    3. Rt.jar in Jresystemlibrary in the project

L Extension Loader

    1. To load an extended class
    2. Ext folder in the JRE
    3. The jar package in the Ext folder in Jresystemlibrary in the project

L System Loader

    1. Be responsible for loading the classes you write
    2. Responsible for loading third-party tool classes

Reflection concept

L run time for any one class

L can all know all the properties and methods of this class.

L can invoke any of his methods and properties for any object

The function of this dynamic method of acquiring information and dynamically invoking objects is called the reflection mechanism of the Java language.

Function
    1. A class that has been completed if you want to add functionality without changing the source of the premise of adding
    2. Dissecting the bytecode file of the new feature class, which is the compiled class file
    3. Called in the source class.

The process of creating a class file

L class loader loading a new feature class produces a class file object

L method of invoking the new function class with this file object

The class file object of the Java.lang.Class class describing the file object is the type of this class

Three ways to get a class file object

L Object Acquisition

L Class name get

L Class-Class static method acquisition

L Three methods The objects obtained are the same

Reflection gets the construction method in the class file

L Run Construction method is equivalent to creating an object

L GetConstructors () gets all the common construction methods in the class file object

L returns an array of type constructors

L Gets a specified constructor method

Variable parameters

L Run

Newinstance ()

L get the constructor method with parameters and run

L Quick Way

    1. The reflected class must have an empty parameter construction method
    2. The construction method must be public

Reflection gets private Construction method

L do not recommend damaging the package security of the program

L Getdeclaredconstructors ()

L Get all the construction methods

L returns an array of type constructor

l Example

L Getdeclaredconstructor (class.. C

L Variable Parameters

L return a private method of construction

l Example

Cannot access private

L Setaccessibale access to permissions

Reflection Get Member Variable

The method in Class GetFields () Getdeclaredfields () is to obtain private

    1. Gets the member variables for all public public classes
    2. return value field[] Array

L GET the specified member variable

Reflection get Null Parameter member method

L GetMethods ()

    1. Gets all the public member methods in the class file, including the inherited
    2. The method class is an object that describes member methods

Many of the methods are public.

L GetMethod ()

    1. Method parameter method name and variable parameter
    2. Returns a Method object

L Use the Obtained method

Invoke ()

    1. Methods obtained by running method
    2. parameter is an object of a class file

Reflection gets the member method with parameters

Reflection-Generic Erase

L store elements outside of the specified generics

l pseudo-Generic compiled class file without generics can get the class file object get method directly

L cannot use strong for or iterator traversal so just to practice reflection

Through the configuration file

L principle

L Implementation

The Java self-study road-day21

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.