Initialization blocks, static initialization blocks, and construction methods in Java

Source: Internet
Author: User

(The so-called initialization method Init () is another thing to do after constructing a method, take care not to confuse)

In Java, there are two types of initialization blocks: static initialization blocks and non-static initialization blocks. They are all defined in the class, enclosed in curly braces {}, and static code blocks are added with the static keyword outside the curly braces.

Non-static initialization block (construction code block):
Function: initializes the object. The object runs as soon as it is established and takes precedence over the operation of the constructor.
Differences from constructors: non-static initialization blocks uniformly initialize all objects, and constructors initialize only the corresponding objects.
Application: Defines what is common to all constructors in the construction code block.

Static initialization blocks:
Function: initializes the class. Executes as the class is loaded and executes only once
differs from building blocks of code:
1) constructs a block of code to initialize the object, which is executed once for each object created, and static blocks are used to initialize the class and execute as the class is loaded, regardless of how many objects are created.
2) Static code block takes precedence over construction code block execution
3) are defined in the class, one with the static keyword, one without the static

constructors, non-static initialization blocks, static code blocks are all used for initialization, and the order of execution of the three is: Static code block > Construction Code block > constructor.
In fact, the initialization block is the addition of the constructor, the initialization block is not able to receive any parameters, the definition of some of the common properties of all objects, methods and so on can be initialized with the initialization block.

The function of static initialization blocks is that when the JVM is loading the class, you want it to do something, then you can initialize the block statically. The order in which these are executed is:

(The JVM loads the class) first loads the static members of the class, and then executes the static initialization block (again, when a class inherits from a class, the parent class is loaded first, and then the load or execution order of the parent class is also described in the sentence).

(When you create an instance of the class) The instance initialization block is executed first, and then the constructor method is executed, but the constructor of the parent class is called first in an inheritance tree, and the order of execution is as described in the sentence.

Example:
Class a{int n = 0;}
Then you create object A = new A (); then you can use the A.N property

Class a{{int a= 0; }  }
Put it inside the block N is not a member of a, you cannot use the A.N property

The former takes N as a member of the object, while the latter simply instantiates an object and declares a local variable a. There are essential differences.

public class Test {
static {
System.out.println ("I am a static block");
}
}
When creating an object of the Test class, such as New Test (), the first is the class load, then the new object, the static block executes when the class is loaded, which means that the static block executes before the new object, and a class is loaded the first time it is used. Then the entire application life cycle will not be loaded again, loading this time, so this means that the static block is executed once, will not be executed twice!


The following is an analysis of the execution flow of the object's constructors and initialization blocks:

Initialization blocks, static initialization blocks, and construction methods 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.