About Jvm class loading mechanism, this one is enough

Foreword

Not updated for a month, this month took place too many things, led to greatly reduce the frequency of updates, anyway clean up the mood, technical studies can not fall!

As each jvm java program ape must understand knowledge, bloggers recommend a book "in-depth understanding of the Java Virtual Machine," before bloggers have seen several times at school, every look there is a new understanding. Plus worked there more than a year's time, it is necessary to sum something ~

What is jvm

Normally we write the code is written in .java files, how to deploy it on a machine running? The jar packaging or by war package, and then run the deployment.

image

If you have read the contents of the jar package so that you know, we write all .java files are compiled into .class files.

It happened here a very important step - to compile : We will translate programs written to be read jvm file format.

Notably, each class will be compiled into a .class file, including the internal and the like. That each .class file corresponds to only a class of our code.

Class life cycle

Class is loaded into the jvm virtual machine memory began to unload a far memory, his life cycle can be divided into: Load -> Authentication -> Prepare -> parse -> Initialization -> Use -> Uninstall.

Let us regard them out:

load

When generating a jar package in the future, we write a program to compile all compiled to jvm can read the .class format. At this point you need to load up, we will load the compiled .class files to the jvm. At this point there will be a "class loader" concept. As shown below.

image

Then a problem, when the class loader will load with a .class jvm? That will load a class under what circumstances?

When running a jar package specifies a main () method as a method of entry. First class will be main () method where loaded into the jvm, the code executed when the encounter went on the add new objects to jvm.

So conclusion, that is, you need to use the time code of this class will be loaded into the jvm.

verification

This need not understand the deep, very straightforward truth, not what Tun Town can be loaded into the jvm, or else went wrong. So at this stage it is to check incoming loads .class file matches a specified rule.

There is a very interesting is that each .class file is very romantic, because each file is .class to eight hexadecimal 0 × CAFEBABE, which translates to coffee baby. Romantic, right? The first step is to check whether the .class file in the verification phase to coffee baby to begin with.

So we can update to the flow chart

image

ready

When we legally put a .class file to load jvm, in which case it will be some preparation.

First, allocate memory space for the class, and then assign a default initial value for class variables (static variables to be modified). But if the final class variables simultaneously modified, then it is not an assignment but the initial value of the specific value

Be described by the following two cases:

public class Student{
    private static int age = 18;
}
//此时就会为age变量分配内存空间并且为其赋值 0 这个初始值。
public class Student{
    private static final int age = 18;
}
//age被final修饰,此时就会为age变量分配内存空间并且为其赋值为 18 。

So we can update to the flow chart

image

Resolve

Jvm parsing stage is the symbolic constant pool references replaced by a direct reference.

Simply means that the code we write, when a variable reference to an object, the references in the .class file is symbolic references to store. It needs to be resolved as a direct reference in the analysis stage. If you have a direct reference to that target reference must already exist in memory.

So we can update to the flow chart

image

initialization

In the preparation phase we have allocated memory space is loaded into the jvm class and gives the initial value for the class variable.

By the initialization phase, it began in earnest the implementation of java program code defined in the class. Mainly in the following steps:

  1. Static variables like giving the correct initial value.
  2. A static block of code class.

Running from top to bottom in the order of class and a static variable assignment statements, and only if the class or interface initialization is the first time they take the initiative to use Java programs. If you have a parent class, first in the order to run in the parent class static variable assignment statements and statements.

So we can update to the flow chart

image

to sum up

In a static method, we can not directly use the non-static variables. When we use the static method, just initialize a class where the static methods, this time only the static variable is assigned a value rather than a static variable is not assigned. So in a static method can not directly use non-static variables. It is my understanding, if misunderstood, welcome bloggers private letter or a message oh ~

Guess you like

Origin www.cnblogs.com/zhxiansheng/p/11128589.html