This is the 23rd day of my participation in Gwen Challenge

Class loading process

  • The class loading process in the JVM is divided into three steps:
    • Load, the Load
    • Links: Link
    • Initialization: Initialize

loading

  • Find and load the binary data for the class

link

  • Validation: To ensure that the loaded classes are correct
  • Preparation: Allocates memory for static variables of the class, which will be initialized to default values
  • Parsing: Converts symbolic references in a class to direct references
  • The reason for the verification steps:
    • First, if the class file is generated by the compiler, it must conform to the JVM bytecode format
    • However, if you use custom class files, loading and running in the JVM can cause security issues
    • Therefore, you need to add validation steps to the class file, and if they don’t, they don’t proceed to ensure JVM safety

Initialize the

  • Assign correct initial values to static variables of the class
  • The preparation and initialization phases may seem contradictory, but they are not:
    • Suppose a class has a statement like this:private static int a = 10 ,This statement is executed as follows:
      • First the bytecode file is loaded into memory
      • Perform the validation steps for the link
      • After the verification succeeds, perform preparation steps to allocate memory to A
      • Since variable A is static, the value of a is the default initial value of type int 0, that is, a = 0
      • Then proceed to the step of parsing
      • It is only at the initialization step that the true value 10 of A is assigned to A, where a = 10

Class initialization

Class initialization scenario

  • When creating an instance of a class, that is, when new a new object
  • Access a static variable of a class or interface, or assign a value to such a static variable
  • When a static method of a class is called
  • Reflection: class.forname (” XxxClass “)
  • When you initialize a subclass of a class, you first initialize the subclass’s parent class
  • The startup class marked when the JVM starts, that is, a class with the same file name and class name

Class initialization steps

  • If the class has not already been loaded and linked, load and link it first
  • If the class has an immediate parent and the class has not been initialized (in a classloader, classes can only be initialized once), the immediate parent is initialized. This does not apply to interfaces
  • Add classes that have initialization statements, such as static variables or static blocks, and execute those initialization statements

Class loading

Class loading process

  • Reads binary data from the class’s.class file into memory
  • Put this data in the method area of the data area at run time
  • Create a java.lang.Class object for this Class in the heap to encapsulate the Class’s object in the method Class
  • The loading of the Class eventually generates the Class object in the heap
    • The Class object encapsulates the data structure of a Class in a method area
    • The Class object provides an interface to access data structures in a method area

How classes are loaded

  • Load directly from the local system
  • Download the. Class file from the network
  • Load. Class files from zip, JAR, etc archives
  • Extract. Class files from a proprietary database
  • Dynamically compile Java source files to.class files, such as servers

Class loader

  • Java class loading is done by ClassLoader and its subclasses

Bootstrap ClassLoader

  • $JAVA_HOME/jre/lib/rt.jar, C++ implementation, not ClassLoader class

Extension ClassLoader

  • It is responsible for loading some JAR packages of extended functions in Java platform, including jre/lib/*. Jar in $JAVA_HOME or jar packages in -djava.ext. dirs specified directory

App ClassLoader

  • Is responsible for loading the jar packages specified in the classpath and the classes in the directory

Custom ClassLoader

  • The application customizes the ClassLoader according to its own needs
  • Both Tomcat and JBoss implement their own ClassLoader according to the J2EE specification

The loading process

  • The classloader first checks to see if the class has been loaded
  • The check sequence is bottom-up, from Custom ClassLoader to BootStrap ClassLoader layer by layer
  • As long as a ClassLoader is loaded, this class is loaded. Ensure that all classLoaders of this class are loaded at least once
  • The loading sequence is top-down, with the upper layer attempting to load classes layer by layer