[JVM Parsing series] [nine] class loading process and initialization of class. How should your class be executed? Why do I need ClassLoader?

Source: Internet
Author: User
Tags array definition field table

Through the previous chapters or detailed or not detailed introduction, we finally put the structure of bytecode analysis is almost. Now we are faced with the question of how to run a bytecode file ?

First of all, the Java language is different from other compile-time need to link the work of the language does not pass, the Java language has a very obvious feature, that is, dynamic loading, a bytecode loading is often in the program when the load comes in, many times this way to bring us convenience. Although in some ways he may have consumed a certain amount of resources to reduce performance.

the life cycle of a class ?

Yes, the life cycle of a class, in many people's eyes, may be born there, along with the program, as the program dies. But this is not the case, the dynamic loading of the Java language requires that a class must have his life cycle, and that the life cycle of a class has 7 phases, as shown in:


The second line in this picture I deliberately put three side by part because the second line can be collectively referred to as a link , which follows the strict starting order, but the resolution is likely to start after initialization, which is also for the dynamic binding of the Java language changes. And there is only the beginning of the relationship, there is no end of the succession relationship.




first of all, let's talk about the first situation. Loading : When to start a class loading process, the JVM specification does not stipulate that different virtual machines have different implementations.

Loading this process is mainly through the permission of a class to get the binary byte stream of the class

And then turn the byte into a method. Data structure of runtime

Finally form a class object in memory

Icon:


We have a strong operability in this is the process of loading bytecode, we are familiar with a method called LoadClass, yes he is used to load bytecode, we can customize a class loader override LoadClass method to control the way the byte stream is loaded, So we can get from the network, get from the database, get from the file, and a dynamic agent technology is generated by the computation.



when the load is complete, it validates .

The main thing to verify is that there is nothing in the class file that doesn't hurt the virtual machine itself. is probably divided into 4 verification processes.

1, file Format verification: We have previously talked about the number of magic and size version is in this position to verify that the constant pool is not all types are supported, Constant_utf8_info is not all UTF8 encoding and so on. The purpose of this phase is to verify that the input byte stream can be correctly parsed, to ensure that the format is correct, if passed this verification to add the byte stream to the method area, the following verification is the validation of the method area. (to several nouns link magic number, size version number, method area)

2, metadata validation: This stage is mainly to see if the metadata conforms to the semantics, like whether the parent inherits the Disallowed class (final Class), does not implement all the methods in the parent class and the interface to implement.

3, bytecode verification; this phase of validation is primarily to see if there are any logical errors, such as a jump command that jumps to the outside of the method.

4, the Symbol reference verification: this people should be very common, it is more common is not able to access a class (not private and protected), through the string description of the permission naming can find the corresponding class.



after the verification, we will begin to prepare .

In the preparation phase, memory is allocated for the class variable and the initial value of the class variable (the static modified variable) is set, and such as static int a = 1, which is not assigned a value of 1 in the preparation phase. Instead, it assigns the most basic initial value of 0, because 1 is called in the constructor of the class when it needs to be initialized.

However, if the field variable is final decorated, the field table will have a Constantvalue attribute (see Constantvalue), at which point the variable will be assigned a value, such as static final int a = 1, and a will be assigned a value of 1;




After preparation, the JVM begins the parsing process.

In the popular sense, parsing replaces the symbolic reference in a constant pool with a direct reference. First, let's explain the meaning of these two nouns.

1. Symbolic references: Symbolic references refer to the use of a set of symbols for reference, the referenced target does not necessarily load into memory

2, direct reference: Direct reference refers to the pointer directly to the target, relative offset or handle.

For parsing, the JVM does not specify when it should be executed, as long as it is parsed before manipulating the symbol reference. So the specific implementation depends on how the JVM is designed (parsing during load or before use)




The next step is to initialize the

As we have said before, the load time of a class depends on the specific virtual machine, but the load validation preparation must be completed when the active reference is encountered

There are five situations in which we call these five kinds of situations Active referencing . (This has been done before the load verification preparation, the following situation is directly initialized can be)

1, when encountering new, getstatic, Putstatic and invokestatic 4 bytecode, corresponding to the Java language is a new class, called a class static field (the final decoration is already in the constant pool, not counting)

2, the use of Java.lang.reflect package for the class for reflection bar when used.

3, Initializes a class, his parent class has not been initialized, the need to first initialize his parent class.

4. The execution class that the virtual machine initiates (that is, the class with the public void main (string[] args) {} method)

5, jdk1.7 after the new features dynamic language support, if a java.lang.invoke.MethodHandle instance of the final parsing results ref_getstatic, ref_putstatic, ref_ Invokestatic method handle, and this class is not initialized.

of course, except for these five active references, all the other methods are not initialized, we call Passive References , let's give a few examples.

1. Referencing a static field of a parent class through a subclass does not trigger the initialization of the subclass.

2, by the array definition to reference the class, does not trigger the initialization of the class, he translated the original class into another class, this class with some array elements of access methods.

3. Constants do not trigger initialization of classes.

process of initialization :

It is time to actually start executing Java code at initialization time, and this time it will execute something called a class constructor (<client> ()) He is responsible for collecting all the static{in the class and merging, in the same order as in the original order, in static{} Only variables that are defined before a static statement block can be accessed, and only values that are assigned to subsequent variables cannot be accessed.

We've talked about it before. When initializing a class, if his parent class has not been initialized, the parent class needs to be initialized first. The <client> () method of the parent class must therefore be better than the subclass execution, which means that the static code block of the parent class is better than the static code block of the subclass. However, the <client> () method of the interface is not necessarily performed before the subclass method, because the parent interface's <client> () method is executed at the time of invocation.

So we can see that throughout the loading process, the part that the programmer can manipulate is just ClassLoader (loading bytecode) and initializing the static code block section. So we're going to talk about ClassLoader next.


[JVM Parsing series] [nine] class loading process and initialization of class. How should your class be executed? Why do I need ClassLoader?

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.