Class loading and class instantiation

The use of classes in Java programs is divided into two categories: active use and passive use

Active use:

  1. Create an instance of the class
  2. Access a static variable of a certain class or interface, or assign a value to the static variable
  3. Call the static method of the class
  4. reflection
  5. Initialize a subclass of a class
  6. The class that is marked as the startup class when the java virtual machine is started
  7. Dynamic language support provided from JDK1.7: the interest calculation result of java.lang.invoke.MethodHandle instance REF_getStatic, REF_putStatic, REF_involeStatic handle corresponding to the class is not initialized, then the initialization is
    used passively:
    except for the above 7 cases, other java classes are applicable The methods are regarded as passive use of the class, and will not cause the initialization of the class.
    Create an instance of the class-create an object
    * Note: A is a class with attributes.
    (1) The memory model when
    declaring an object uses A a; when declaring an object a, memory space will be allocated for the reference variable a of the object in the stack memory, but the value of A is empty, which means that a is an empty object. The empty object cannot be used because it has not referenced any "entities".
    (2) The memory model
    when the object is instantiated. When a=new A(3,5); is executed, there will be two processes of connection and initialization.
    Connection : Connection is divided into three small steps: verification, preparation, and analysis. In the preparation process, the JVM will allocate memory for the member variables of the class in the heap memory, and initialize it to the default value of each data type; then perform explicit initialization (initialization value during class definition), at this time, when it arrives Before initialization, class variables are not initialized to real initial values;
    resolution: The resolution process is to find the symbolic references of classes, interfaces, fields, and methods in the constant pool of types, and replace these symbolic references with direct references.
    Initialization : Finally, call the constructor to assign values ​​to the member variables. Return the reference of the object in the heap memory (equivalent to the first address) to the reference variable a, and then you can refer to the object in the heap memory through a. This process assigns the initial value to the class variable.
    (3) Create multiple different object instances
    A class can create multiple different object instances by using the new operator. These object instances will be allocated different memory spaces in the heap. Changing the state of one object will not affect the state of other objects. E.g:
A  a1= new A(3,5);
A  a2= new A(4,6);

At this time, memory space will be allocated for the member variables of the two objects in the heap memory, and the space occupied by the two objects in the heap memory is different from each other. If there is:

A  a1=new A(3,5);
A  a2=a1;

Then only one object instance is created in the heap memory, two object references are created in the stack memory, and the two object references point to one object instance at the same time.

Guess you like

Origin blog.csdn.net/weixin_45457983/article/details/107130062