Java Review Basics-code execution sequence

Source: Internet
Author: User

Code block: In Java, use the code enclosed in {} to become a block of code

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 of multiple construction methods.

Static code block: The position of a member in a class, with the code enclosed in {}, except that it is decorated with static

Function: class is generally initialized

public class Blocktest {
{
System.out.println ("444 blocktest Construction code block (top)!" ");
}

public static void Main (string[] args) {
{
System.out.println ("222 Local code block run! ");
}
System.out.println ("333 blocktest Main Method executed");
Blocktest blocktest = new Blocktest ();
Coder cd = new Coder ();
Coder CD2 = new Coder ();
}

static {
System.out.println ("111 blocktest static code block is executed");
}

{
System.out.println ("555 Blocktest construction code block is executed (bottom)");
}

Public Blocktest () {
System.out.println ("666 blocktest non-parametric construction execution");
}
} class Coder {

static {
System.out.println ("777 coder static code block is executed");
}

{
SYSTEM.OUT.PRINTLN ("888 Coder Construction code block is executed");
}

Public Coder () {
SYSTEM.OUT.PRINTLN ("999 coder non-parametric construction execution");
}
}

Execution order Result:

"1" Static code block

Function: Initializes the class, and if a class contains more than one static block of code, it is executed in the order in which the code executes.

1. Executes as the class loads, and only once.

2. Takes precedence over the main function execution.

3. When a class enters memory, static code is loaded first, such as static code blocks, variables modified with static, functions, and so on. Static blocks of code are used to initialize classes. A constructor is the initialization of an object, while a static code block initializes the class, such as changing the static variable num to his value in a static code block. Static blocks of code can also be useful in situations where you do not need to instantiate a class.

"2" Construction code block

In the class:

Format: {Construct code block execution statement}

Function: initializes all objects.

Note what time: 1. Every time you create an object, he will execute it.

2. Construction code is faster than constructor execution.

3. The difference between building blocks and constructors is that building blocks of code is a unified initialization of all objects, and constructors are initialized to the corresponding objects, because constructors are multiple, and what objects are created by which constructors run, but regardless of which object is created, the same construction code block is executed first. In other words, the initialization content of different object commonalities is defined in the Construction code block.

"3" Local code block

Local code block, also known as normal code block. It is a block of code that acts in a method.

Function: Is the life cycle of the control variable.

In the program when we define a local variable x, and in the next code, no longer want to use it, then there is no need for X to continue to occupy space in memory. Therefore, there is a local code block.

Interview questions?

Static block of code, Construction code block, the execution order of the construction method?

Static code blocks > Constructing code blocks > Construction Methods

Static code block: Execute only once

Construction method: Each call to the constructor method executes

Java Review Basics-code execution sequence

Related Article

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.