Class B extends A, and then class A, which is the parent class, has static code blocks, normal code blocks, static methods, static member variables, normal member variables, normal methods. Subclasses do the same thing, and then after inheritance, print out the result of the program. Involves the initialization order of various members of a Java class. After testing, the following conclusions are obtained:

1. The parent class [static block] and [static member] are executed in the order in which they appear in the code. 2. Subclasses [static blocks] and [static members] are executed in the order in which they appear in the code. 3. Ordinary code blocks and ordinary member variables assigned by ordinary member methods of the parent class are executed in the order they appear in the code. 4. Execute the constructor of the superclass. 5. Ordinary code blocks and ordinary member variables assigned by ordinary member methods are executed in the order in which they appear in the code. 6. Execute the subclass constructor.

Here’s a diagram of the test code and the results:

The parent class:

package com.test.ClassInitTest;

public class Parent {
    static {
        System.out.println("Parent class: Static code block");
    }

    {
        System.out.println("Parent class: Normal code block");
    }

    private static String staticStringInParent = initStaticStringInParent();

    private String stringInParent = initStringInParent();

    public Parent(a) {
        System.out.println("Superclass: Constructor");
    }

    private static String initStaticStringInParent(a) {
        System.out.println("Superclass: static method called by static member variable assignment.");
        return "initStaticStringInParent";
    }

    private String initStringInParent(a) {
        System.out.println("Superclass: ordinary member methods called by assignment to ordinary member variables.");
        return "initStringInParent"; }}Copy the code

The subclass:

package com.test.ClassInitTest;

public class Child extends Parent {

    private static String staticStringInChild = initStaticStringInChild();
    private String stringInChild = initStringInChild();

    static {
        System.out.println("Subclasses: Static code blocks");
    }

    {
        System.out.println("Subclasses: Normal code blocks");
    }

    public Child(a) {
        System.out.println("Subclasses: constructors");
    }

    private static String initStaticStringInChild(a) {
        System.out.println("Subclasses: static methods called by static member variable assignment.");
        return "initStaticStringInChild";
    }

    private String initStringInChild(a) {
        System.out.println("Subclasses: ordinary member methods called by assignment to ordinary member variables.");
        return "initStringInChild"; }}Copy the code

Test the main method

package com.test.ClassInitTest;

public class ClassInitTest {
    public static void main(String[] args) {
        System.out.println("Test code start");
        new Child();
        System.out.println("End of test code"); }}Copy the code

Result output:

Test code starts parent: static code block Parent: static method, called by static member variable assignment. Subclasses: static methods that are called by static member variable assignments. Subclass: Static code block Parent class: normal code block Parent class: normal member method, called by normal member variable assignment. Superclass: Constructor subclass: ordinary member methods that are called by assignment to ordinary member variables. Subclass: Normal code block Subclass: Constructor tests the end of codeCopy the code