Home  >  Article  >  Java  >  Example analysis of execution sequence of java code blocks

Example analysis of execution sequence of java code blocks

WBOY
WBOYforward
2023-04-18 12:07:031738browse

1. The execution flow of static code blocks, structural code blocks, and structural methods of the class

Static code block>structural code block=display initialization (see the order)>structure method.

2. Static content is loaded with class loading.

Static code block content is executed first.

3. Initialize the parent class before initializing the subclass.

Initialization sequence of class member variables: explicit initialization is consistent with the initialization level of the structure code block, so the code sequence determines the initialization order, but note that the structure code block cannot add data types.

Example

class Fu {
    static {
        System.out.println("静态代码块Fu");
    }
 
    {
        System.out.println("构造代码块Fu");
    }
 
    public Fu() {
        System.out.println("构造方法Fu");
    }
}
 
class Zi extends Fu {
    static {
        System.out.println("静态代码块Zi");
    }
 
    {
        System.out.println("构造代码块Zi");
    }
 
    public Zi() {
        System.out.println("构造方法Zi");
    }
}
 
class ExtendsTest2 {
    public static void main(String[] args) {
        Zi z = new Zi();
    }
}

The above is the detailed content of Example analysis of execution sequence of java code blocks. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete