"IT168 Technology" when you run a Java program by calling the Java command, the command launches a Java Virtual machine process, all threads of the same JVM, all variables in the same process, all using the memory area of the JVM process.
- The program runs to the last normal end.
- The program runs to the end of the program using the System.exit () or runtime (). exit () code.
- An uncaught exception or error was encountered during the execution of the program.
- The platform on which the program is located forces the JVM process to end.
Loading of classes
Class loading refers to reading class files into memory and creating a Java.lang.Class object for them, and all of the classes in the system, which are actually objects, called Class objects, are instances of Java.lang.Class. Virtual machines manage a unique class object for each type, meaning that each class (type) has a class object.
Class of connections
The connection phase will be responsible for merging the class's binary data into the JRE
- Verify
- Preparation: The class preparation phase is responsible for allocating the static properties of the class to memory and setting the default initial value
- Analytical
Initialization of the class
During the initialization phase of a class, the virtual machine is responsible for initializing the class, primarily to initialize the static property, and to specify the initial value for the static property in the Java class in two ways: (1) specifying the initial value when declaring a static property, and (2) setting an initial value for a static property using a static initialization block
The class or interface is initialized when the Java program first uses one of the following 6 ways to use a class or interface.
A) Create an instance of the class.
b) Call a static method of a class.
c) Access the static property of a class or interface, or assign a value to the static property.
d) Use reflection to force the creation of a Java.lang.Class object corresponding to a class or interface.
E) Initializes a subclass of a class that, when a subclass of a class is initialized, all the parent classes of that subclass will be initialized.
f) When running a main class directly using the Java.exe command, the program initializes the main class first.
Class Loader
The class loader is responsible for loading the. class file into memory and generating the corresponding Java.lang.Class object for it.
In Java, a class is identified with its fully qualified class name, including the package name and class name.
In the JVM, a class is uniquely identified with its fully qualified class name and its class loader.
When the JVM starts up, it forms a three-class loader:
- Bootstrap ClassLoader: Root ClassLoader It is not a subclass of Java.lang.ClassLoader, but is implemented by the JVM itself Rt.jar.
- Extension ClassLoader: Extension class loader It is responsible for loading the class package of the jar in the extension directory ext of the JRE.
- System ClassLoader: Systems ClassLoader It is responsible for loading the-classpath option from command Java or Java.class.path System Properties, or the jar package and classpath specified by the CLASSPATH environment variable when the JVM is started. The default is to use the current path as the system load path.
Appclassloader: User-defined class loader.
JVM class loading mechanism
Overall responsibility
Parent class Delegate: The so-called parent class delegate attempts to load the class from the parent class loader before attempting to load it from its own classpath only if it is unable to load it.
Caching mechanism
Viewing class information by reflection
There are three ways to get a class object in a Java program:
A) Use the class forname () static method. The method needs to pass in a string parameter that is the fully qualified class name of a class (the full package name must be added).
b) Call the Class property of a category to get the class object for that category.
c) Call the GetClass () method of an object, which is a method in the Java.lang.Object class, so that all Java objects can call the method, which returns the class object to which the object belongs.
B Method: The code is more secure, and the program can check the existence of the class object that needs to be accessed during the compile phase.
Program performance is improved because this method does not need to call the method, so performance is better.
Once you have the class object corresponding to a category, you can call the class object's method to get the object and the real information for that class.
Getdeclared is explicitly declared regardless of the access level.
Get gets all but just public, including inherited ones.
Using reflection to build and manipulate objects
The class object can get the methods (represented by the Methode object) that are included in the classes, the constructor (represented by the constructor object), Field (Field object representation), and the three classes are defined under the Java.lang.reflect package. and implements the Java.lang.reflect.Member interface, the program can use method object to execute corresponding methods, through the constructor object to call the corresponding constructor to create objects, can directly access and modify the object's property values through the Field object.
There are two ways to generate objects by reflection:
A) Use the Newinstance () method of the class object to create an instance of the class object corresponding to its class, which requires that the corresponding classes of the class object have a default constructor, while executing the newinstance () method actually creates an instance of the class with the default constructor.
b) Use the class object to get the specified constructor object, and then call the Newinstance () method of the constructor object to create an instance of the class object's corresponding classes, in this way you can choose to create an instance using the development constructor of one of the classes.
Reflection is only considered when the program needs to create the object dynamically, and it is often possible to use reflection when developing a broad-based framework and base platform.
When a class object is obtained for one of the classes, it is possible to get all or the specified method through the class object's GetMethods () method or the GetMethod () method----The return value of both methods is an array of method objects, or a method object.
Each method object contains a way to get the method object, and the program can call the corresponding methods by using it, including an invoke method in the method.
Obejct invoke (Object obj, Object ... args), the method in which obj is the keynote that executes the method, and later args is the argument that is passed in when the method is executed.
When invoking the method by means of the Invoke method, Java requires that the program must have permission to invoke the method, and if the program does need to invoke the Invoke method of an object, you can call the method object as follows:
Setaccessible (Boolean flag): Sets the accessible flag of the flag object to the indicated Boolean value.
True indicates that the method should remove the Java language Access check when it is in use.
Accessing property values
The GetFields () or GetField () method of the object allows you to get all the field (attributes) that the class contains, or the specified Field,field provides the following two sets of methods to access the properties:
GetXxx (Object obj): Gets the property value of the field of the Obj object
Setxxx (Object obj, Xxx val): Sets the field of the Obj object to a Val value
Use these two methods to freely access all properties of the specified object, including the properties of private access control
Pre-access checks must be canceled by Setaccessible (true)
manipulating arrays
An array class is also provided under the Java.lang.reflect package, and the array object can represent all arrays
Generating a JDK dynamic agent using reflection
A proxy class and a Invocationhandler interface are provided under the Java Java.lang.reflect package to generate a JDK dynamic proxy class or dynamic proxy object by using this class and interface.
Proxy provides
Static object Newproxyinstance (ClassLoader loader, class[] interfaces, Invocationhandler h): Creates a dynamic proxy object directly, The implementation class of the proxy object implements the series interface specified by interfaces, and the Invoke method that executes the Invocationhandler object is substituted for each method that executes the proxy object.
In general, when we use proxy to generate a dynamic agent, we often do not produce a dynamic agent in thin air, usually to create a dynamic proxy for the target object.
This dynamic proxy is called an AOP agent in AOP (Aspect Orient program, which is aspect-oriented programming), an AOP proxy replaces the target object, and the AOP proxy contains all the methods of the target object, but the methods in the AOP proxy differ from the methods of the target object: The methods in the AOP proxy can be used before the target method is executed and then some generic processing is inserted.
Java Program class loading and its reflection mechanism