Java static code block, construction code block, and execution sequence of Construction Methods

Source: Internet
Author: User

Java static code block, construction code block, and execution sequence of Construction Methods
Java static code block, construction code block, and execution sequence of Construction Methods

Static Code takes precedence over non-static codeThe reason is that the static modified members are all class members and will be executed when the JVM loads the class. The members not modified by static are also called instance members, objects must be created before they are loaded to the heap memory. Therefore, static data takes precedence over non-static data.
The constructor (constructor) exists before the execution method body.Implicit three-step:
1. super statement, The following three situations may occur:
1) if the first line of the constructor is the this statement, three implicit steps are not executed,
2) If the first line of the constructor body is a super statement, the constructor of the corresponding parent class is called,
3) if the first line of the constructor is neither this nor super, super () is called implicitly, that is, the default constructor of its parent class, this is also why a parent class usually provides default constructor;
2. initialize non-static variables;
3. Construct a code block.
We can see that,The construction code block takes precedence over the method body of the construction method.But this and super keywords cannot appear at the same time, and can only appear in the first line of the Code. If the keyword "this" is displayed, the three steps are implicitly not executed.
For example, after analyzing the following code and execution results, you have marked Step 1-Step 7 with annotations.
That is to say, when multiple constructor methods are called recursivelyLast (that is, the first line of the method body is not the one of this statement)The constructor is executed before execution!

Public class Test {public static int a = 0; static {// Step 1 a = 10; System. out. println (the static code block is executing a = + a);} {// Step 4 a = 8; System. out. println (non-static code block (construct code block) is executing a = + a);} public Test () {this (call construction method 1 with parameters, a = + ); // Step 2 System. out. println (the construction method without parameters is executed in a = + a); // Step 7} public Test (String n) {this (n, call the construction method with parameters 2, a = + a); // Step 3 System. out. println (Construction Method 1 with parameters in the execution of a = + a); // Step 6} public Test (String s1, String s2) {System. out. println (s1 ++ s2); // Step 5} public static void main (String [] args) {Test t = null; // The JVM loads the Test class, static code block execution System. out. println (new Test instance); t = new Test ();}}

Execution result:

A static code block is a new Test instance under the execution of a = 10: a non-static code block (constructed code block) is executed in the execution of a = 8 call with parameters constructor 1, a = 10; call construction method 2 with parameters, a = 10 with parameters constructor 1 in execution a = 8 without parameters constructor in execution a = 8

 

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.