Home  >  Article  >  Java  >  An in-depth analysis of ordinary code blocks, constructed code blocks and static code blocks in Java

An in-depth analysis of ordinary code blocks, constructed code blocks and static code blocks in Java

高洛峰
高洛峰Original
2017-01-18 15:03:561348browse

//Execution order: (Priority from high to low.)

Static code block>mian method>Construction code block>Construction method.

The static code block is only executed once. The construction code block is executed every time an object is created.

1. Ordinary code block

public static void main(String[] args) {
/*普通代码块:
*直接定义在在方法或语句中出现”{普通代码的执行语句}“的就称为普通代码块。
*普通代码块执行顺序由他们在代码中出现的次序决定--“先出现先执行”
* */
{
  System.out.println("这里是普通代码块A");
}
//new A();
{
  System.out.println("这里是普通代码块B");
}
}

Execution result: This is ordinary code block A
  This is ordinary code block B

2 .Static code block and constructed code block

A code block declared using the static keyword in java.

Commonly used to initialize a class, each static code block will only be executed once (executed when the class is loaded in memory, the class already exists after the class is loaded in memory) because the JVM will execute static code when loading the class code block, so the static code block is executed before the main method. If the class contains multiple static code blocks, the code defined first will be executed first, and the code defined later will be executed.

ps:

1 Static code blocks cannot exist in any Method body.

2 Static code blocks cannot directly access static instance variables and instance methods. They need to be accessed through the instance object of the class.

Construction block: defined directly in the class without adding static. The code block of the keyword is called the {} construction code block.

The construction code block is called when an object is created, and is called every time an object is created, and the execution order of the construction code block takes precedence over the class constructor.

public class structure {
{
System.out.println("这里是普通代码块");//所有类中有一个默认的构造函数,这里的代码块为构造代码块,在类中的对象被创建时执行
}
public static void main(String[] args) {
/*普通代码块:
*直接定义在在方法或语句中出现”{普通代码的执行语句}“的就称为普通代码块。
*普通代码块执行顺序由他们在代码中出现的次序决定--“先出现先执行”
* */
{
System.out.println("这里是普通代码块A");
}
new structure();//第二次类加载时静态代码块不执行
//new A();
{
System.out.println("这里是普通代码块B");
}
}
static{
System.out.println("这里是静态代码块");
}
}

Execution result:

This is a static code block //Priority to the main function
This is an ordinary code block A
This is an ordinary code block//In class It is executed when the object is created. It is executed once every time it is created. Add new structure(); and the execution result is:

Here is the ordinary code block B

3. Summary summary

public class structure {
{
System.out.println("这里是普通代码块");
}
public static void main(String[] args) {
{
System.out.println("这里是普通代码块A");
}
//new structure();
//new structure();
new A();
{
System.out.println("这里是普通代码块B");
}
}
static{
System.out.println("这里是静态代码块");
}
}
class A{
static{
System.out.println("这里是A中的普静态代码块1");
}
{
System.out.println("这里是A中的普通代码块1");
}
{
System.out.println("这里是A中的普通代码块2");
}
}

Execution results:

Here is the static code block
Here is the ordinary code block A
Here is the common static in A Code block 1
Here is the normal code block 1 in A
Here is the normal code block 2 in A
Here is the normal code block B

Priority summary: Static code block> Main()>Construction code block

For more in-depth analysis of ordinary code blocks, construction code blocks and static code blocks in Java, please pay attention to the PHP Chinese website


#! ##

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn