Interesting Discussion on Java Class Loading Process

First, let's hang a life cycle diagram of class loading

Picture source: Interviewer: Why can't static methods in java call non-static methods and variables?
Class loading process image

Specific process explanation:

  • 1. Loading : "Loading" is the first process of "class plus mechanism". In the loading phase, the virtual machine mainly completes three things:
    • Get the binary byte stream defined by a class by its fully qualified name
    • Convert the static storage structure represented by this byte stream into the runtime data structure of the method area
    • A Class object representing this class is generated in the heap as the access entry for these data in the method area.

Note that at this time, we will scan to see whether there are static variables or static methods in our code, and these static data structures have not been allocated.

  • 2. Verification : The main function of verification is to ensure the correctness of the loaded class.

  • 3. Preparation : The preparation phase mainly allocates memory for class variables and sets initial values. These memories are allocated in the method area. Note that memory will be allocated for our class variables, that is, static variables, but the ordinary member variables have not yet been.

  • 4. Parsing : The parsing stage is mainly a process in which the virtual machine converts symbol references in the constant pool into direct references.

  • 5. Initialization : This is the last step of the class loading mechanism. At this stage, the java program code starts to execute. We know that the class variable has been assigned a value in the preparation phase. At the initialization stage, programmers can assign values ​​according to their needs. We will assign values ​​to our ordinary member variables during initialization.

Next we start to have fun

  • 1. Loading : The Tesla class needs to be executed on the Shanghai JVM, so the first step is to give it to him, right, three things need to be done:
    • Shanghai needs to use the fully qualified name of a class (Tesla, USA...) to obtain its defined binary byte stream (Tesla's construction plan drawings).
    • Convert the static storage structure (structure on the drawing) represented by this byte stream into the runtime data structure of the method area (the actual land structure of a certain area in Shanghai)
    • Shanghai has set up a special project team to deal with Tesla's establishment of a factory. This project team records the situation of all Teslas (this project team represents Tesla's Class objects), the project team has always existed (disappeared with Tesla), and other people visited Tesla Instead of going directly to the actual Tesla factory.
  • 2. Verification : The main function of verification is to ensure that this Tesla is really a Tesla and not a developer who fakes money to obtain land (to prevent security vulnerabilities caused by code injection).
  • 3. Preparation : Start to divide the land area for the Tesla factory, and investigate the actual land in Shanghai where the A and B areas on the drawing will fall (static variable allocation of memory), as for the Tesla store (example (Object) Don’t worry, these storefronts will be selected based on the size of the flow of people in downtown Shanghai, so it’s not necessary to mention the sample cars placed in the storefront (ordinary member variable).
  • 4. Analysis : The symbolic reference is transformed into the actual reference. This is a bit complicated. Explain it like this: The symbolic reference is the symbol on the Tesla factory drawing, but the person who moved the brick doesn't know it, so he asked the designer. The designer said, these Ask the executive designer for the details. The executive designer said that this was provided by the XX outsourcing company. You asked the XX company. Finally, you went to the XX company to ask. This symbol is a special code toilet (actual reference), so you It took a lot of effort to convert all symbolic references into actual references.
    • Here are two points :
      • If there is a direct reference, then the target of the direct reference must be loaded into memory (indicating that you must have asked what this symbol actually means).
      • Why is it not good to use symbolic references directly? When compiling, each java class will be compiled into a class file, but the virtual machine does not know the address of the referenced class when compiling, so it uses symbolic reference instead, and in this parsing stage, this stage transformed into symbolic references real address (compile those people planning to run when the executor is true, they are two groups of people, who compiled not able to consider the actual situation of running, which is why there will be compiled Error and runtime error).
  • 5. Initialization : This is the last step before Tesla goes into production. At this stage, the java program code starts to be executed. We know that in the preparation stage, a value has been assigned to the class variable (allocation of actual land to the planned area). At the initialization stage, programmers can assign values ​​according to their needs (meaning two points:
    • 1. Assigned before, now I change the value.
    • 2. There was no assignment before, now I will assign it).
      Note that if there is no assignment before, if it is not assigned now, the compilation period will not pass.
      At the same time, the store is also initialized at this time, and the variables of our ordinary members (Tesla prototype) will be assigned during initialization.

Welcome to make a brick, I will correct it!
Also advertise my github project: AlgorithmPractice.
This project aims to establish a knowledge structure system for easy search. More like-minded friends are welcome to join AlgorithmPractice (issues and pull requests are welcome)

Guess you like

Origin blog.csdn.net/ljfirst/article/details/105363449