Loading and reflection of Java learning classes

Source: Internet
Author: User

Class loading mechanism and reflection

(i) loading, joining, and initialization of classes

1, a main function is equivalent to invoking a JVM process, and the main function ends its memory data cleanup

2, class loading (class initialization) Three steps:

1: Load of class:

2, the connection of the class:

3, initialization of the class:

(ii) Class loader

1,bootstrap ClassLoader: Root classloader (non-classloder subclass, JVM supplied)

2,extension ClassLoader: The extension class loader is responsible for loading classes in the JRE

3,system ClassLoader: The jar package and classpath specified by the system ClassLoader CLASSPATH environment variables

4, user-defined class (bottom-up preference)

5, class loading mechanism: first cache to find, then the parent class to find, and finally the overall search

(iii) through reflection-based information and manipulating objects

1. View the class information: class name clazz=, and then call Clazz to view the class information

2, creating object methods by reflection mechanism

3. Gets the class member variable that is the same as the production object. Getdeclazredfield (object) Gets the member variable, returns the field type, and then changes the value by the Field.set/get method

(iv) using reflection to produce a JDK dynamic agent

1. Create a dynamic proxy using proxy and Invocationhandler

Java Test Code: (Handling events on Invork)

 PackageClassLoader;ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method; Public classMyinvokationhandlerImplementsInvocationhandler { Publicobject Invoke (Object proxy, Method method, object[] args)throwsThrowable {//TODO auto-generated Method StubSYSTEM.OUT.PRINTLN ("----runing Methond" +method); if(args!=NULL) {System.out.println ("The arguments passed in when the method is executed:");  for(Object Val:args) {System.out.println (val); }        }        Else{System.out.println ("Call method has no arguments"); }        return NULL; }}
View Code
 PackageClassLoader;ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Proxy; Public classProxytest { Public Interfaceperson {//Internal Interface        voidWork (); voidSayHello (String name); }     Public Static voidMain (string[] args) {//TODO auto-generated Method StubInvocationhandler handler=NewMyinvokationhandler (); Person P= (person) proxy.newproxyinstance (person).class. getClassLoader (),NewClass[]{person.class}, handler);        P.work (); P.sayhello ("Xiao Ming"); }}
View Code

Loading and reflection of Java learning classes

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.