1. Overview
1.概述
In this tutorial, we’ll learn the concept of static block and instance initializer block. We’ll also check the differences and the execution order of the class constructors and initializer blocks.
在本教程中,我们将学习静态块和实例初始化块的概念。我们还将检查类构造器和初始化块的区别和执行顺序。
2. Static Block
2.静态块
In Java, a static block executes code before the object initialization. A static block is a block of code with a static keyword:
在Java中,静态块在对象初始化之前执行代码。静态块是一个带有static关键字的代码块。
static {
// definition of the static block
}
Static initializer block or static initialization block, or static clause are some other names for the static block. Static block code executes only once during the class loading. The static blocks always execute first before the main() method in Java because the compiler stores them in memory at the time of class loading and before the object creation.
静态初始化块或静态初始化块,或静态条款是静态块的一些其他名称。静态块代码在类加载过程中只执行一次。在Java中,静态块总是先于main()方法执行,因为编译器在类加载时和对象创建前将其存储在内存中。
A class can have multiple static blocks, and they will execute in the same order as they appear in the class:
一个类可以有多个静态块,它们将按照它们在类中出现的相同顺序执行。
public class StaticBlockExample {
static {
System.out.println("static block 1");
}
static {
System.out.println("static block 2");
}
public static void main(String[] args) {
System.out.println("Main Method");
}
}
The output for the above code snippet is:
上述代码片断的输出是。
static block 1
static block 2
Main Method
Here, the compiler executes all the static blocks first, and after finishing the static block execution, it invokes the main() method. The Java compiler makes sure that the execution of static initialization blocks will be in the same sequence as they appear in the source code.
在这里,编译器首先执行所有的静态块,在完成静态块的执行后,它调用main()方法。Java编译器确保静态初始化块的执行将与它们在源代码中出现的顺序相同。
Static blocks of parent class execute first because the compiler loads parent class before child class.
父类的静态块首先执行,因为编译器在子类之前加载父类。
As a curiosity, before Java 1.7, the main() method wasn’t mandatory in every Java application, so all the code could be written within static blocks. However, from Java 1.7 onwards, the main() method is mandatory.
有趣的是,在Java 1.7之前,main()方法在每个Java应用程序中不是强制性的,所以所有的代码都可以写在静态块中。然而,从Java 1.7开始,main()方法是强制性的。
3. Instance Initializer Block
3.实例初始化块
As the name suggests, the purpose of the instance initializer block is to initialize the instance data members.
顾名思义,实例初始化块的目的是初始化实例数据成员。
The instance initializer block looks just like the static initializer block, but without the static keyword:
实例初始化块看起来就像静态初始化块,但没有static关键字。
{
// definition of the Instance initialization block
}
Static initializer blocks always execute before the instance initialization blocks because static blocks run at the time of class loading. However, the instance block runs at the time of instance creation. The Java compiler copies initializer blocks into every constructor. Therefore, multiple constructors can use this approach to share a block of code:
静态初始化块总是在实例初始化块之前执行,因为静态块在类加载时运行。然而,实例块在实例创建时运行。Java编译器将初始化块复制到每个构造函数中。因此,多个构造函数可以使用这种方法来共享一个代码块。
public class InstanceBlockExample {
{
System.out.println("Instance initializer block 1");
}
{
System.out.println("Instance initializer block 2");
}
public InstanceBlockExample() {
System.out.println("Class constructor");
}
public static void main(String[] args) {
InstanceBlockExample iib = new InstanceBlockExample();
System.out.println("Main Method");
}
}
So, in this case, the output for the above code would be:
因此,在这种情况下,上述代码的输出将是。
Instance initializer block 1
Instance initializer block 2
Class constructor
Main Method
The instance initializer blocks execute during every constructor invocation since the compiler copies the initializer block in the constructor itself.
实例初始化程序块在每次构造函数调用时执行,因为编译器将初始化程序块复制到构造函数本身。
The compiler executes the parent class’s instance block before executing the current class’s instance block. The compiler invokes the parent class constructor by super(), and instance blocks execute at the time of constructor invocation.
编译器在执行当前类的实例块之前执行父类的实例块。编译器通过super()调用父类的构造函数,而实例块则在构造函数调用时执行。
4. Differences Between Static and Instance Initializer Block
4.静态和实例初始化块之间的区别
Static Block | Instance Initializer Block |
It executes during class loading | It executes during class instantiation |
It can only use static variables | It can use static or non-static (instance variables). |
It can not use this | It can use this |
It executes only once during the entire execution of the program when the class loads into the memory | It can run many times whenever there is a call to the constructor |
5. Conclusion
5.总结
In this tutorial, we have learned that the compiler executes static blocks during class loading. Static blocks can be used to initialize static variables or to call a static method. However, an instance block is executed every time an instance of the class is created, and it can be used to initialize the instance data members.
在本教程中,我们已经了解到,编译器在类的加载过程中会执行静态块。静态块可以用来初始化静态变量或调用静态方法。然而,每次创建类的实例时都会执行一个实例块,它可以用来初始化实例数据成员。
In addition, the complete code samples for this article can be found over on GitHub.
此外,本文的完整代码样本可以在GitHub上找到over。