08-02 Java code block, code block execution sequencing issues

Source: Internet
Author: User

code block

/*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 blocks: local locations, which are used to define 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.        Interview questions?        Static blocks of code, building blocks of code, constructing the order in which the methods are executed? Static block of code-constructs a block of code-constructs a static code block: Constructs a block of code only once: every call to the constructor method executes*/classCode {Static {        intA = 1000;    System.out.println (a); }    //Building Code Blocks    {        intx = 100;    SYSTEM.OUT.PRINTLN (x); }        //Construction Method     PublicCode () {System.out.println ("Code"); }        //Construction Method     PublicCode (inta) {System.out.println ("Code"); }        //Building Code Blocks    {        inty = 200;    System.out.println (y); }        //Static code block    Static {        intb = 2000;    System.out.println (b); }}classCodedemo { Public Static voidMain (string[] args) {//Local code block        {            intx = 10;        SYSTEM.OUT.PRINTLN (x); }        //symbol not found//System.out.println (x);        {            inty = 20;        System.out.println (y); } System.out.println ("---------------"); Code C=NewCode (); System.out.println ("---------------"); Code C2=NewCode (); System.out.println ("---------------"); Code C3=NewCode (1); }}

Look at the program, write the results:

/*write the execution result of the program. Parsing process: When loading class, first execute a, main main method, execute B, new student () First, load student class, execute C, new student () The first time, execute E, then execute d.        Because the construction method is placed before the code block is constructed, the construction code block new student () is executed the second time before, no more C is executed, because the static code block executes the second time with new student (), executes E, and then executes D. Execution order: Brigitte is 60, and I'm sad. I am the Main method---Student static code block Student construct code block Student construction Method Student construct code block Student Construction Method*/classStudent {//C    Static{System.out.println ("Student Static code block"); }        //D     PublicStudent () {System.out.println ("Student Construction Method"); }        //E{System.out.println ("Student Construction code block"); }        }classStudentdemo {Static {        //ASystem.out.println ("Brigitte is 60, I'm sad."); }         Public Static voidMain (string[] args) {//BSystem.out.println ("I am the Main method"); Student S1=NewStudent (); Student S2=NewStudent (); }}

08-02 Java code block, code block execution sequencing issues

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.