Detailed explanation of class loading connection and initialization process

Continue to follow the topic of class loading from the last time [ http://www.cnblogs.com/webor2006/p/8447586.html ] and continue to learn. This time it is not pure theory, and it will involve code demonstration, so hurry up and start.

Class loading, connection and initialization:

Let's use a picture to review the complete process described earlier:

Next, enter the learning of new knowledge:

  • Java programs can use classes in two ways:
     ① Active use
       ② Passive
    use
  • All JVM implementations must initialize each class or interface when it is " first actively used " by a Java program.
    The description marked in red shall meet two conditions: 1. When using it actively; 2. When using it for the first time.
  • Active use ( seven )
    ①, create an instance of the class.
    ②, access a static variable of a class or interface, or assign a value to the static variable.
    ③, call the static method of the class.
    Note: Regarding the second and third points, from the Java bytecode mnemonics [you can use the javap command to view the relevant information, the JVM provides a lot of mnemonics, so it is unrealistic to remember, and you don't need to remember it. For the important understanding, it is possible to understand the level: accessing the static variables of a class is represented by getstatic, and assigning a static variable is represented by putstatic, and calling a static method of a class is represented by invokestatic.
    ④, reflection (such as Class.forName("com.test.Test")).
    ⑤, initialize a subclass of a class.
      If there are two classes, one is Parent and the other is Child, as follows:

          The initialization of Child will also initialize Parent.
    ⑥. The class that is marked with the startup class when the JVM starts, which means the class that contains the main method.
    ⑦. The dynamic language support provided by JDK1.7: the parsing result of the java.lang.invoke.MethodHandle instance REF_getStatic, REF_putStatic, REF_invokeStatic The corresponding class of the handle is not initialized, it will be initialized. 【You can understand】
  •  In addition to the above seven cases, other ways of using Java classes are regarded as passive use of the class , and will not lead to the initialization of the class .

Let's use a specific example to feel the active use and passive use. For a new project, first define two classes, as follows:

Since I want to study class, I define statically related stuff, and then define a static code block in the subclass:

 Then use it in the main function:

What is the result at this time?

So strange, what's the reason? The reason is because " for a static field, only the class that directly defines the field will be initialized ", back to this program, because the str static field is defined in MyParent1, so only the active use of MyParent1 will not Active use of MyChild1, that is, to demonstrate the above sentence:

Then modify the program to:

What will be the result then?

Because str2 is defined in MyChild1, it will be initialized if it is actively used, but also remember that among the seven active usage methods mentioned earlier, this one belongs to:

That is, when a class is initialized, all its parent classes are required to be initialized, so the static code block will be executed when the parent class is initialized, so the output is as shown above.

Class loading [further details]:

  •  Class loading refers to reading the binary data in the .class file of the class into memory, placing it in the method area of ​​the runtime data area, and then creating a java.lang.Class object in memory (the specification and It is not specified where the Class object is located. Oracle's HotSpot virtual machine puts it in the method area) to encapsulate the data structure of the class in the method area.
  • The way to load the .class file:
    1. Load directly from the local system. [The most common way]
    ②, download the .class file through the network.
    ③. Load .class files from zip, jar and other archive files. [This is why the three-party libraries are usually provided in the form of jar packages]
    ④. Extract .class files from proprietary data. [Use less, you can understand]
    ⑤. Compile Java source files dynamically into .class files. [Dynamic proxy is the most typical example]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324395087&siteId=291194637