Java notes--understanding Java ClassLoader and ClassLoader classes

Source: Internet
Author: User
Tags instance method

class Loader Overview:

Java class loading is done by the virtual machine, the virtual machine to describe the class file loaded into memory, and data validation, parsing and initialization, the final form can be used directly by the Java Virtual Machine Java type, which is the virtual machine class loading mechanism. The specific implementation of this function in the JVM is the classloader . The classloader reads the. class bytecode file to convert it to an instance of the Java.lang.Class class. Each instance is used to represent a Java class. Through the newinstance of the instance () method to create an object of that class.

the life cycle of a class:

The life cycle of classes from loading to virtual machine memory to being freed from memory is as follows:

load : "Load" is a phase of the class loading process, and the functions that are completed in this phase are:

  Gets the binary byte stream that defines this class through the fully qualified name of the class

Transforms the static storage structure represented by this binary byte stream into the runtime data structure of the method area

The Java.lang.Class object that represents this class is generated in memory as the class accesses the mouth.

validation : The first step in the connection phase. The purpose of the verification is to ensure that the information in the byte stream of the class file conforms to the requirements of the virtual machine and does not compromise the security of the virtual machine so that the virtual machine is protected against malicious code. Roughly complete the following four check actions:

File Format Verification

Source data validation

BYTE code Verification

Symbol Reference Validation

Preparation : The second step in the connection phase is to formally allocate memory for the class variable and set the initial value of the variable. (Contains only class variables and does not contain instance variables).

parsing : The third step in the connection phase, the virtual machine replaces the symbolic reference in the constant pool with a direct reference, and the parsing action is primarily for classes or interfaces, fields, class methods, method types, and so on.

initialization : The initialization of a class is the last step in the class loading process, in which the Java program code defined in the class is actually started. This phase executes the class constructor.

use : Use the functionality provided by this class.

unload : Release from memory.

get the class file path:Java classes can be dynamically loaded into memory, which is a major feature of Java, also known as runtime binding, or dynamic binding.

1. Reading from a ZIP package is common and eventually becomes the basis for future jar,war,ear formats.

2. Obtained from the network, this scenario is typical of the applet.

3. Runtime compute generation, typical scenario is Java Dynamic Agent technology.

4. Generated from other files, a typical scenario is a JSP application that generates a corresponding class class from a JSP file.

java.lang.ClassLoader class overview:The ClassLoader class is defined in the Chinese document as follows:

   

The introduction of the ClassLoader class from the documentation summarizes the function of this class to find the corresponding class bytecode file based on the fully qualified name of a specified class, and then load it into an instance of the Java.lang.Class class.

class Loader Partitioning:Most Java programs use the following 3 system-provided ClassLoader:

   Start the class loader (Bootstrap ClassLoader):

    This classloader is responsible for loading the class library in the <java_home>\lib directory into the virtual machine memory, which is used to load the Java core Library, which does not inherit from Java.lang.ClassLoader and cannot be called directly by a Java program. The code is written in C + +. Is part of the virtual machine itself.

   Extension class loader (extendsion ClassLoader):
   
This classloader is responsible for loading the class library under the <java_home>\lib\ext directory, which is used to load the JAVA extension library, and developers can use the ClassLoader directly.

   Application class loader (application ClassLoader):

This classloader is responsible for loading the class library under the user classpath (CLASSPATH), which is typically loaded by this classloader, which is the return value of the Getsystemclassloader () method in the ClassLoader. This is also known as the system ClassLoader. Typically this is the default class loader for the system.

In addition, we can add our own defined ClassLoader to meet special requirements and inherit the Java.lang.ClassLoader class.

the hierarchical relationships between class loaders are as follows:

   

look at the class loader using code:
 Package com.wang.test;  Public class Testclassloader {    publicstaticvoid  main (string[] args) {        = Testclassloader. class . getClassLoader ();        System.out.println (Loader.tostring ());        System.out.println (Loader.getparent (). toString ());        System.out.println (Loader.getparent (). GetParent ());}    }

To observe the printed result:

[Email protected]
[Email protected]
Null

The first line prints the Application class loader (the default loader), the second line prints its parent classloader, the extension classloader, according to our idea the third line should print the startup ClassLoader, and here it returns NULL, because GetParent (), return null, The Startup class loader is used as the parent loader by default.

The parent delegation model of the class loader:

The parent delegation model is a specification of the relationship between the organization ClassLoader, and he works by saying that if a class loader receives a request for a class load, it does not attempt to load the class itself, but instead delegates the request to the parent ClassLoader to complete, so that the layer is progressive, Eventually all of the load requests are uploaded to the topmost startup ClassLoader, and only when the parent ClassLoader is unable to complete the load request (it does not find the required class within its search scope) is handed to the subclass loader to try to load.

The benefit is that the Java class has a hierarchical relationship with precedence along with its classloader. This is necessary, such as Java.langobject, which is stored in \jre\lib\rt.jar, which is the parent class of all Java classes, So no matter which class is loaded to load the class, eventually all the load requests are rolled up to the top-level startup ClassLoader, so the object class is loaded by the startup ClassLoader, so the same class is loaded, and if the parent delegation model is not used by each classloader itself, There will be more than one object class in the system, and the application will be completely messed up.

Class.forName () and Classloader.loadclass ():

Class.forName (): Is a static method, most commonly used is Class.forName (String className), and returns a class object based on the fully qualified name of the passed-in class. This method, while loading the class file into memory, The initialization of the class is performed.

such as: class.forname ("Com.wang.HelloWorld");

Classloader.loadclass (): This is an instance method that requires a ClassLoader object to invoke the method, which loads the class file into memory and does not perform initialization of classes. This class is not initialized until it is first used. The method requires a ClassLoader object, so you can specify which class loader to use as needed.

such as:ClassLoader cl= ...; Cl.loadclass ("Com.wang.HelloWorld");

  

Note: Finishing this note, referring to < in-depth understanding of Java Virtual Machine > This book, as well as in-depth discussion of Java ClassLoader This article, want to know more can go to see.

Java notes--understanding Java ClassLoader and ClassLoader classes

Related Article

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.