java class loading process

Class loading process

Demo code:

public class Demo01 {
    public static void main(String[] args) {
        Demo demo = new Demo();
        //3、创建对象
        demo.test01();
        //18、调用test01方法
    }
}

class Demo{
    static int a;
    //1、a=0
    int b;
    //4、demo.b=0
    int num2 = test01();
    //5、调用test01方法
    //9、demo.num2=1
    static{
        System.out.println("1"+a);
        //2、打印10
    }
    {
        System.out.println("2"+a);
        //10、打印21
        System.out.println("2"+b);
        //11、打印21
    }
    public Demo(){
        System.out.println("3"+a);
        //12、打印31
        System.out.println("3"+b);
        //13、打印31
        test01();
        //14、调用test01方法
    }

    public int test01(){
        System.out.println("4"+(a++));
        //6、打印40,a=1
        //15、打印41,a=2
        //19、打印42,a=3
        System.out.println("4"+(b++));
        //7、打印40,b=1
        //16、打印41,b=2
        //20、打印42,b=3
        return a;
        //8、返回1
        //17、返回2
        //21、返回3
    }
}

Results: Here Insert Picture Description
1, all the properties of static load, allocated memory block

2, sequentially property assignment, and then perform a static block

3, to create an object is the same reason

4, give the member variable division of space, followed by the assignment

5, due to the static part of the already loaded with class and fully loaded into memory, it is not executed. Performing code block structure

6, then the constructor is executed

Huawei is said to be the subject of n years ago

public class Demo02 {
    //1、将所有static修饰的属性,代码块和方法加载进内存
    //2、对static修饰的属性进行初始化
    public static int k = 0;
    //3、k=0
    public static Demo02 t1 = new Demo02("t1");
    //4、创建一个Demo02对象为t1赋值,static修饰的属性和代码块已经加载进内存,不再执行
    public static Demo02 t2 = new Demo02("t2");
    //17、与t1同理,结果为:4:j i=3 n=3;5:构造块 i=4 n=4;6:t2 i=5 n=5
    //t2.a=0 t2.j=3
    public static int i = print("i");
    //18、调用print方法,为i赋值,此时i=6
    //22、i=7
    public static int n = 99;
    //23、n=99
    private int a = 0;
    //5、k=0 t1.a=0
    public int j = print("j");
    //6、调用print(String str)为j赋值
    //t1.j=1
    {
        print("构造快");
        //10、调用print()函数
    }
    static{
        print("静态块");
        //24、调用print方法
    }
    public Demo02(String str){
        System.out.println((++k)+":"+str+"  i="+i+"  n="+n);
        //14、k=3,i=2,n=2; 3:t1 i=2 n=2
        ++i;
        //15、i=3
        ++n;
        //16、n=3
    }
    public static int print(String str){
        System.out.println((++k)+":"+str+" i="+i+" n="+n);
        //7、k=1;i,n还没赋值,默认值为0; 1:j i=0 n=0
        //11、k=2;i=1,n=1; 2:构造块 i=1 n=1
        //19、k=7;i=6,n=6;7:j i=6 n=6
        //25、k=8;i=7,n=99;8:静态块 i=7 n=99
        ++n;
        //8、n=1
        //12、n=2
        //20、n=7
        //26、n=100
        return ++i;
        //9、i=1,返回1
        //13、i=2,返回2
        //21、i=7,返回7
        //27、i=8,返回8
    }
    public static void main(String[] args) {
        //28、结束
        //i=8 n=100
    }
}

result:Here Insert Picture Description

See code execution sequence code comments

1, the difficulty lies in the face questions initialize static member variables to create an object, you need to understand the class loading mechanism.


The class loading mechanism

Class loading mechanism is divided into three parts: load, link initialization. Links can be subdivided into verification, preparation and resolution.

load

It refers to the class bytecode files from various sources by the class loader loaded into memory.

verification

In order to ensure compliance with load incoming byte stream virtual machine specification, it will not pose a security error. (I also do not understand this principle, only know that there is such a thing)

ready

(Interview questions test center) mainly as a static variable (note not the instance variables) to allocate memory and Initialization. The initial value, the code is not specific to write initialization value, but the Java virtual machine default initial value depending on the type of the variable. The basic type of the initial value is 0, the initial value of a reference type is null, the initial value is the constant value code set.

Explanation

The constant pool replace references to direct reference symbols. (I also do not understand this principle, only know that there is such a thing)

initialization

(Interview questions test sites) at this stage of the modified static variables, initialization code block. If the initialization of a class, parent class has not been initialized, initialize the parent class. If the static variables and comprising a plurality of code blocks, it is executed from top to bottom.


expand:

Memory is divided into four areas:

Stack: the stack of stored basic data types and references to variables defined from the object (not an object) **, objects stored in the heap itself, the method being executed is to pull the stack when executing the method of and then push the stack.

Heap: all objects created using new in itself are stored on the heap

Data area: storing methods in the class

Code area: static String variables and constants

If any mistakes, please correct me.

Released five original articles · won praise 1 · views 218

Guess you like

Origin blog.csdn.net/qq_41551830/article/details/104454324