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