java-the difference between a common code block, a construction code block, and a static code block.

Source: Internet
Author: User

Execution order: Priority from high-Low: Static code block-"Main method-" Construction code block-"construction method.

1, ordinary code block. (can be executed sequentially)

2, constructs the code block. (a block of code that is defined directly in a class and does not have a static keyword is called a construction code block.) Construction blocks are called when the object is created, each time the object is created, and the order in which the code block is constructed takes precedence over the class constructor. )

3, static code block.

Static code block: a block of code declared in Java using the static keyword. Static blocks are used to initialize the class and initialize the properties of the class. Each static block of code is executed only once. Because the JVM executes static blocks of code when the class is loaded, the static block of code executes before the main method.
If a class contains more than one static block of code, it will follow the code defined first, then execute after the definition.
Note: 1 Static code blocks cannot exist in any method body. 2 Static code blocks do not have direct access to instance variables and instance methods, and need to be accessed through instance objects of the class

For more information, see: http://www.cnblogs.com/sophine/p/3531282.html (Sophine)

java-the difference between a common code block, a construction code block, and a static code block.

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.