is Java static code block really going to take precedence?

Source: Internet
Author: User
public class b{public static B t1 = new B ();    public static B t2 = new B ();    {System.out.println ("building Block");    } static {System.out.println ("static Block");    } public static void Main (string[] args) {b t = new B (); }} 



Correct answer    building block construction block static block construction block

static block: With static declaration, the JVM executes when the class is loaded, executes only one construction block: The class is defined directly with the {}, execution order priority is executed each time the object is created: static block > Main () > Construction block > Construction method static blocks are executed in the order of declaration, so the PUBLICSTATICB T1 =&NBSP;NEWB () is executed first, the statement creates the object, and the construction block is called, and the output construction block is then executed Public  static  B T1 = new < Span style= "Color:rgb (51,51,51)" >
B (); output building block re-execution static{system.out.println ("static Block");} Output static block the last Main method executes, creates the object, and outputs the construction block.

copyright notice: This article for Bo Master original article, without Bo Master permission not reproduced.

Does Java static code block really take precedence?

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.