] [Java class configuration and loading objects (instantiated) and the process sequence

1. Load class

1.1 What kind of load the load? (Node class is loaded)

A. found on the hard drive Node.class, parse the contents of the file, generate the Node class, the Node class information stored in the memory area method

1.2 What happens the next time the class is loaded trigger? And the class is not in memory

(1) loaded on demand (lazy loading)

          . A instantiate an object class new Node (1);

          b. Static properties are class or static method Main.merge (...)

          c. subclass used, there must be the parent class new CNode

         d. Load Static first code block parent class, then a static load block subclass

 

1.3 Other rules

Initialization sequence static property:

By writing sequential code, execution

a. initialization static attribute definition

b. Static code

2. Code block

Block of code defines: using "{}" defines a section of code;

Code block is configured to follow the object and loaded loaded.

Common code block 2.1

Common code block: the method defined in the code block;

public class Test1 {
    public static void main(String[] args) {
        {
            //普通代码块
            int a=10;
            System.out.println(a);
        }
        int a=20;
        System.out.println(a);
    }
}

Building blocks 2.2

Building blocks: the class defined in the code block (without modifier)

class A{
    //定义在类中,不加任意修饰符
    {
        System.out.println("A的构造块");
    }
    //构造方法
    public A(){
        System.out.println("A的构造方法");
    }
}
public class Test2 {
    public static void main(String[] args) {
        new A();
        new A();
    }
}

The code can be found by: building block method execution priority to the configuration, each new object is to produce a building block is called once. So building blocks can be simple logical operation (before calling the constructor).

2.3 static code block

Static block of code: static definition blocks of code, which is followed by loading of the loaded classes.

Depending on which is located a static code block can be divided into the following two types:

1. In the non-primary class static code block

2. static block of code in the main category of

2.3.1 Static code block non-primary class

class B{
    //构造块
    {
        System.out.println("B类的构造块");
    }
    //代码方法
    public B(){
        System.out.println("B类的构造方法");
    }
    //静态代码块
    static {
        System.out.println("B类的静态代码块");
    }
}
public class Test3 {
    public static void main(String[] args) {
        System.out.println("==============================");
        new B();
        new B();
        System.out.println("==============================");
    }
}

It can be found:

                1, performed when the class is loaded static block of code in the main class of non-priority block configured to execute

                2. No matter how many instances of an object is generated, the static code block is executed only once

Static block main role is to initialize static properties!

2.3.2 static block of code in the main category of

public class Test4 {
    //构造块
    {
        System.out.println("Test4类的构造块");
    }

    //构造方法
    public Test4(){
        System.out.println("Test4类的构造方法");
    }
    //静态代码块
    static {
        System.out.println("Test4类的静态代码块");
    }

    public static void main(String[] args) {
        System.out.println("===========================");
        new Test4();
        new Test4();
        System.out.println("===========================");
    }
}

Defined in the main category static block, a method in preference to the main (main) are performed in the JVM starts, when executed compiled JVM will first main class is loaded, this time the main class Static block executes.

3. The configuration of the object / instantiate new Node

Rule 3.1

1. Priority initialization properties of the parent class, (and following the same order of execution)

2. The initialization is performed in the following order

a. A code written in order, and configured to perform a defined code blocks

b. constructor is executed again

3. static block of code is executed only once when loaded, to perform a static class parent blocks, and then perform a static block subclass.

3.2 regard to the implementation of the order

1. The parent class subclasses loaded before

2. The parent class constructor is called before the subclass

Initialization is performed 3.static attribute (performed in the order) when loading classes

1) initializing defined

2) configured initialization block

4. The common attributes is executed when the initialization configuration of the object (execution order)

1) initializing defined

2) configured initialization block

3) initializing constructor

5. Only use class, will load the class

class Hello{
    //构造方法
    public Hello(){
        System.out.println("1.Hello父类的构造方法");
    }
    //非静态代码块
    {
        System.out.println("2.Hello父类非静态代码块");
    }
    //静态代码块
    static{
        System.out.println("3.Hello父类静态代码块");
    }
}
public class HelloA extends Hello{
    public HelloA(){
        System.out.println("4.helloA构造方法");
    }
    //非静态代码块
    {
        System.out.println("5.HelloA非静态代码块");
    }
    //静态代码块
    static{
        System.out.println("6.HelloA静态代码块");
    }

    public static void main(String[] args) {
        System.out.println("7.---start---");
        new HelloA();
        new HelloA();
        System.out.println("8.---end---");
    }
}

Mentioned above execution order to execute: first used as a main class HeoolA, so static loaded first code block;

Results of the:

class Hello1{
    //构造方法
    public Hello1(){
        System.out.println("1.Hello1父类的构造方法");
    }
    //非静态代码块
    {
        System.out.println("2.Hello1父类非静态代码块");
    }
    //静态代码块
    static{
        System.out.println("3.Hello1父类静态代码块");
    }
}
class Hello2 extends Hello1 {
    public Hello2() {
        System.out.println("4.hello2构造方法");
    }

    //非静态代码块
    {
        System.out.println("5.Hello2非静态代码块");
    }

    //静态代码块
    static {
        System.out.println("6.Hello2静态代码块");
    }

}
public class Test{
    public static void main(String[] args) {
        System.out.println("7.---start---");
        new Hello2();
        new Hello2();
        System.out.println("8.---end---");
    }
}

operation result:

 

Note: The contents of this article with reference to a block of code ! ! !

 

 

 

 

Published 62 original articles · won praise 9 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43669007/article/details/101542363