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

Example analysis of execution sequence of java code blocks

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-04-18 12:07:03
forward
1846 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:
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
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template