1 Object instantiation process

  • The instantiation process of an object is divided into two parts: class load initialization and object initialization
  • To create an object instance of a class, you need to load and initialize the class, and the class on which the main method is located
  • Class initialization is performed using the

    method, and object instantiation is performed using the

    method

  • To initialize a subclass, you need to initialize the parent class

Class 2 loading process


  • Class loading mechanism: if there is no corresponding class, the class is loaded into the method area. Corresponding to the load -> verify -> Prepare -> parse -> initialization phase
    • Load: Loads a class object, not necessarily from a class file, but from a JAR package or dynamically generated class
    • Validation: Verifies that the Class byte stream complies with current JVM specifications
    • Preparation: Allocates memory for class variables and sets their initial (default) values. An assignment declares a value if the object is final
    • Parse: Replace symbolic references to the constant pool with direct references
    • Initialization: Executes the class constructor

      (note not the object constructor), assigns values to class variables, and executes static code blocks. The JVM ensures that the

      of a subclass completes before the

      of its parent class executes


  • The three parts of verification, preparation and resolution are called connection
  • The < Clinit > method consists of static variable assignment code and static code blocks; Execute the class static variable display assignment code first, then the static code block code

3 Conditions for triggering class loading

  • The first time a new object of a class is created, the load initialization of the class and the execution of the object’s initialization function

    are triggered. This is instance initialization. The other six are class initializations
  • The JVM starts with a class that initializes the main method
  • Invokestatic methods of the class (such as executing the invokestatic directive)
  • Perform read and write operations on static fields of a class or interface (that is, execute getStatic, putStatic directives); Except for static fields with final modifications (already assigned, String, and primitive types, no wrapper types included), it is initialized as a compile-time constant expression
    • Note: When working with a static field, only the class that directly defines the field is initialized; Manipulating a static field defined in a parent class through its subclass only triggers initialization of the parent < Clinit > class and not of the subclass
  • When calling reflection methods in the JavaAPI (than calling methods in java.lang.Class (class.forname), or other classes in the java.lang.Reflect package)
  • When initializing a class whose parent is not initialized, the parent must be initialized first (except for interfaces).

4 Object instantiation process

  • Object instantiation processEssentially, it executes the class constructor corresponding to the <init>() method in the bytecode file (called the instance constructor); The < init > () methodNon-static variables, non-static code blocks, and corresponding constructors
    • The

      () method can be overloaded as many times as the class has constructors
    • The code in the

      () method is executed in the order: superclass variable initialization, superclass code block, superclass constructor, subclass variable initialization, subclass code block, subclass constructor.
  • Static variables, static code blocks, normal variables, normal code blocks, constructor execution order
  • Subclasses with parent classes are instantiated in the following order

Class loaders and the Parent Delegate rule, how to break the parent Delegate Rule

  • Class loader
    • The code module that implements this action is called a class loader
    • Each class needs its loader and the class itself to determine its uniqueness within the JVM; Each class loader has its own class namespace, and if the same class is loaded by different loaders, the JVM considers it to be different classes
  • Parental delegation model
    • The startup class loader is implemented in C++ code as part of the virtual machine. Responsible for loading libraries under \lib
    • Other class loaders have Java language implementations, are independent of the JVM, and inherit from classloaders
    • Extention ClassLoader is responsible for loading the class libraries in the \lib\ext directory
    • The application ClassLoader is responsible for loading code in the user’s ClassPath
    • Loading the same class file by different classloaders results in two classes. The Java solution is that the lower class loader and delegate to the higher class loader to load the class. If the parent class fails to load (the corresponding class cannot be found in its own directory), the lower class loader is returned to load the class. The following figure
  • Break the parental delegation model
    • The parental delegation model is not a mandatory constraint model, but rather a class loading implementation recommended by Java designers to developers
    • Parents delegation model good solve various class loading base class of the same problem (the base class by the upper level of the loader loads), but the base class is always as the user code calls the API, but if it is the concrete implementation of the lower code, code in the lower base class at this time you need to call, you need to break the parents delegation model
    • For JNDI services, the JNDI code has a startup class to load (rt.jar), which calls the Service Provider Interface (SPI) code of JNDI deployed by an independent vendor in the application classpath. To solve the SPI code loading problem, Java introduced the thread context classloader to load SPI code. That is, the parent class loader asks the subclass to complete the loading of the class
    • Thread-context classloader, which is inherited from the parent thread when it is created, defaults to Application Class Loader if global scope is not set

Corrections are welcome

Pay attention to the public number, communicate together

Refer to the article

  • Object instantiation process
  • The instantiation of a Java object