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

Example analysis of execution sequence of java code blocks

WBOY
Release: 2023-04-18 12:07:03
forward
1815 people have browsed it

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();
    }
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template