Loading process for Java classes

Source: Internet
Author: User


compile : The Javac process, which compiles. java files into a. class file, which is compiled into a bytecode file, with some type and format checks.


class is loaded into the JVM only whenit is to be run, that is, it will only be loaded into the JVM when it needs to be compiled, and this dynamic loading is based on reflection, in general a class will only be loaded once, The next time it is fetched from the JVM's class cache, it will not go to the filesystem to get the class file.


The following is a specific class loading process:

classes are loaded in two ways:

1. Implicit loading: When we create an object through new, he implicitly invokes the class loader to load the class into the JVM. Unable to dynamically load, that is, your new class object must be the time you compile your program code.

2. Explicit loading: A class that is loaded with class.forname () to display. This allows for dynamic loading, which you can load into a class that is not available at compile time while the program is running.

class Loader (responsible for loading the class into the JVM)

There are three kinds of loaders, Why should have three class loader, on the one hand is the division of labor, each responsible for their respective blocks, on the other hand, in order to implement the delegation model, then encountered to load a certain class, three class loader between how to work, specifically see another article " Deep understanding of Java ClassLoader ClassLoader.

How the class loader works, we can actually implement a class loader ourselves:

try {   URL url = new  URL ("file:/d:/test/lib/");   According to the path urlclassloader URLCL = new  urlclassloader (new  Url[]{url});   Class C = Urlcl.loadclass ("Testclassa");   Testclassa object = (Testclassa) c.newinstance ();   Object.Method ();   } catch (Exception e) {   e.printstacktrace ();   }   

From the above code should be able to basically know the implementation of the ClassLoader.


Next, we'll talk about the whole process of class loading:

When we want to execute a. class file, Java.exe will help us find the JRE, Then find the Jvm.dll in the JRE, this is the Java virtual machine, after the virtual machine activation, will do some initialization action, once the initialization action is completed, the first class loader--bootstrap Loader (which is written by C + +), and then Bootstrap Loader Loads the Extclassloader loader in Launcher.java and sets its parent to null (because its parent is Bootstrap Loader, it is written by C + +, The instance could not be found ), and then Bootstrap Loader asked to load the Appclassloader loader in Launcher.java and set its parent to

Extclassloader. So the Extclassloader loader and the Appclassloader loader are loaded by Bootstrap Loader, and the parent is not related to who is loading it.

With the ClassLoader, how to work together between these three loaders can be seen in the deep understanding of Java ClassLoader ClassLoader. As for how to load the. class file into the JVM, you can also see the custom class loader from above.



The loading process is pretty much the same, but a class has to be linked and initialized with two procedures.

loaded ----and the link to initialize the link also includes the validation----"three processes

1, loading (that is, the whole process mentioned above)

The load phase of a class is primarily to obtain a binary byte stream that defines this class, and to convert the static storage structure represented by this byte stream into the runtime data structure of the method area, and finally generates a Java.lang.Class object representing the class in the Java heap as the access entry for the data in the method area. The load phase is the most controllable phase in the development phase, relative to other stages of the class loading process. We can control the way binary byte stream is obtained by customizing the class loader, which is classloader.

2. Verification

validation, preparation and parsing are all part of the connection phase, and validation is the first step in the connection phase. This phase is primarily to ensure that the information contained in the byte stream of the class file is combined with the requirements of the current virtual machine and does not compromise the security of the virtual machine itself. The main validation processes include file format validation, metadata validation, bytecode validation, and symbolic reference validation.

3. Preparation

The preparation phase formally allocates memory for class variables and sets initial values. The initial value here is not the initialized value, but the default 0 value of the data type. the class variable mentioned here is a static modified variable , not an instance variable. The only exception to setting the 0 value for a class variable at the prep stage is when the class variable is also final decorated, and at compile time, the target value is assigned directly to the constant.

4. Analysis

When parsing, a virtual machine replaces a symbolic reference in a constant pool with a direct reference procedure.

5. Initialize

  1) When instantiating an object with the New keyword, when a static field of a class is read or set (except by the final decoration), and when the static     method of a class is called (for example, the constructor is a static method), the class is initialized first if it is not initialized.  
  2) when a class is called through a reflection mechanism, it is initialized first if the class is not initialized.  
  3) The parent class is initialized first if its parent class is not initialized at the beginning of the first class.  
  4) The user-specified execution main class (the class with the Main method) is initialized first when the virtual machine is started.

In addition to the above 4 ways, all referencing a class does not trigger initialization, called a passive reference. For example, referencing a static field of a parent class through a subclass does not cause a subclass to be initialized; referencing a class through an array definition does not trigger initialization of such a class; A static constant of a reference class does not trigger the initialization of the class that defines the constant, because the constants are already placed in the constant pool during the compilation phase.



Above is a class loading and can use the entire process, Java class loading this only when needed to load the practice of memory to save a lot of space.


Loading process for Java 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.