[Turn] The daily life of Java programmers - the order in Java class loading

Source: http://www.cnblogs.com/xing901022/p/5507086.html

 

I mentioned the loading order of classes in Java before. After reading the inheritance part this time, let’s talk about the loading order of classes in combination with inheritance.

Inherited load order

Since the static block is executed when the class is first loaded, the following example uses the static block to test the loading order of the classes.

package xing.test.thinking.chap7;
class A{
    static{
        System.out.println("A static");
    }
}
class B extends A{
    static{
        System.out.println("B static");
    }
}
class C extends B{
    private static D d = new D();
    static{
        System.out.println("C static");
    }
}
class D{
    static{
        System.out.println("D static");
    }
}
public class ExtendTest {
    public static void main(String[] args) {
        C c = new C();
    }
}

In the above example, class C inherits B, B inherits A, and C has dependencies on D. Therefore, when C is created, the B inherited by C and the dependent D will be automatically loaded, and then B will load the inherited A. Only after A is loaded, B can be loaded smoothly; after BD is loaded, C can be loaded. This is the load order of the classes.

A static
B static
D static
C static

After all variables are initialized, the constructor will be executed

During the loading process of a class, the constructor of the class will be executed only after the internal variables are created.

package xing.test.thinking.chap7;
class A2{
    B2 b2 = new B2();
    static{
        System.out.println("A static");
    }
    public A2() {
        System.out.println("A2()");
    }
}
class B2{
    C2 c2 = new C2();
    D2 d2 = new D2();
    static{
        System.out.println("B static");
    }
    public B2() {
        System.out.println("B2()");
    }
}
class C2{
    static{
        System.out.println("C static");
    }
    public C2() {
        System.out.println("C2()");
    }
}
class D2{
    static{
        System.out.println("D static");
    }
    public D2() {
        System.out.println("D2()");
    }
}
public class VarTest {
    public static void main(String[] args) {
        A2 a2 = new A2();
    }
}

In the above example, A2 has the B2 variable inside, and B2 has the C2D2 variable. Therefore, when the class is loaded or read first, the corresponding static block is executed.
When all dependent objects are defined, the constructor will be executed:

A static
B static
C static
C2()
D static
D2()
B2()
A2()

The difference between static member and ordinary member class loading

In the class loading process, the objects of static member classes will be loaded first, while the objects of ordinary member classes will be loaded only when they are used.

package xing.test.thinking.chap7;
class A3{
    B3 b3 = new B3();
    static C3 c4 = new C3();
    static{
        System.out.println("A3");
    }
}
class B3{
    static{
        System.out.println("B3");
    }
}
class C3{
    static{
        System.out.println("C3");
    }
}
public class StaticTest {
    public static void main(String[] args) {
        A3 a3 = new A3();
    }
}

output:

C3
A3
B3

Summarize

The first point is that all classes will load the base class
first. The second point is that the initialization of static members takes precedence.
The third point is that the constructor will only be executed after the members are initialized.
The fourth point is that the initialization of static members and the execution of static blocks occur in when the class is loaded.
Fourth, the creation of class objects and access to static blocks will trigger class loading.

Order of supplementary class constructors

Look at the code:

package xing.test.thinking.chap8;
class A{
    public A() {
        System.out.println("A");
    }
}
class B extends A{
    public B() {
        System.out.println("B");
    }
}
class C extends B {
    private D d1 = new D("d1");
    private D d2 = new D("d2");
    public C() {
        System.out.println("C");
    }
}
class D {
    public D(String str) {
        System.out.println("D "+str);
    }
}
public class ExtendTest {
    public static void main(String[] args) {
        C c = new C();
    }
}

Results of the:

A
B
D d1
D d2
C

Therefore it can be concluded that:

  • The constructor of the base class is called first
  • Second, call the member's constructor
  • Finally, call your own constructor

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326647422&siteId=291194637