code block execution order in Java

Source: Internet
Author: User

Code blocks: In Java, code enclosed in {} is called a code block.
Depending on its location and declaration, it can be divided into


Local code block: a local location that defines the life cycle of a variable.
Construct code block: The position of the member in the class, with the code enclosed in {}. The construction code block is executed before each call to the construction method execution.
Function: You can put together the common code in multiple construction methods to initialize the object.
Static code block: The position of a member in a class, with the code enclosed in {}, except that it is decorated with static.
Function: The class is typically initialized.


Static blocks of code, building blocks of code, constructing the order in which the methods are executed?

Execute static code block first

Then execute the construction code block

Last point to construct method

Class fu{    static {        System.out.println ("Fu Static Code");    }    {        System.out.println ("Fu Code");    }    Public Fu () {        System.out.println ("Fu Gouzao");}    } Class Zi extends fu{    static {        System.out.println ("Zi Static Code");    }    {        System.out.println ("Zi Code");    }    Public Zi () {        System.out.println ("Zi Gouzao");}    } public class text{public    static void Main (string[] args) {        Zi Zi = new Zi ();}    }

Output:

Fu static Codezi static codefu Codefu Gouzaozi Codezi Gouzao

  

code block execution order in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.