Home >Java >javaTutorial >Detailed introduction to instance initialization block (IIB) in Java (code example)
This article brings you a detailed introduction (code example) about the instance initialization block (IIB) in Java. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.
Class Initialization Block in Java Language In this article, we briefly introduced the instance initialization block (IIB) in Java. However, I think the introduction is a bit simple, so I will write another article to introduce it in detail.
In the Java language, there are three operations: methods, constructors and initialization blocks.
The initialization block is divided into instance initialization block (IIB) and static initialization block. In this chapter, we mainly introduce the instance initialization block.
Instance initialization block is used to initialize instance variables.
The instance initialization block will be executed when initializing an instance of the class, and will be executed before the constructor. And they are executed every time an object of the class is created.
The syntax of the instantiation block
The instance initialization block is generally placed before the constructor, using curly braces {}
to expand the code. The syntax structure is generally as follows
class JavaTester { // 实例化块 { System.out.println("IIB block"); } // 构造函数 JavaTester() { System.out.println("Constructor Called"); } public static void main(String[] args) { JavaTester a = new JavaTester(); } }
Note that instantiation blocks are not required for classes. Just an option. It is used to extract the public part of the constructor and execute it separately.
This code can be compiled and run, and the running results are as follows
[yufei@www.twle.cn java]$ javac JavaTester.java && java JavaTester IIB block Constructor Called
A class contains multiple instance initialization blocks
# in the class ##Instance initialization block is not required for the class, and there is no limit to the number. A class can have no instance initialization block or multiple instance initialization blocks.
If a class has multiple instance initialization blocks, they will be executed in order from top to bottom, that is, the instance initialization block defined at the top of the class is executed first. Let's look at a piece of code. The following class JavaTester defines multiple instance initialization blocks.class JavaTester { { System.out.println("IIB1 block 1 "); } { System.out.println("IIB2 block 2"); } JavaTester() { System.out.println("Constructor Called"); } { System.out.println("IIB3 block 3"); } public static void main(String[] args) { JavaTester a = new JavaTester(); } }Run the above code, the output result is as follows
[yufei@www.twle.cn java]$ javac JavaTester.java && java JavaTester IIB1 block 1 IIB2 block 2 IIB3 block 3 Constructor Called
Initialization block in the parent class
In the inheritance system of the Java language, the parent Classes can also have initialization blocks, and there is no limit to the number. The instance initialization block of the parent class is run immediately after callingsuper() in the constructor of the child class. The compiler will execute the instance initialization block of the parent class before executing the instance initialization block of the current class.
class B { B() { System.out.println("B-Constructor Called"); } { System.out.println("B-IIB block"); } } public class JavaTester extends B { { System.out.println("JavaTester IIB1 block 1 "); } { System.out.println("JavaTester IIB2 block 2"); } JavaTester() { super(); System.out.println("JavaTester-Constructor Called"); } { System.out.println("JavaTester IIB3 block 3"); } public static void main(String[] args) { JavaTester a = new JavaTester(); } }Run the above example, the output result is as follows
[yufei@www.twle.cn java]$ javac JavaTester.java && java JavaTester B-IIB block B-Constructor Called JavaTester IIB1 block 1 JavaTester IIB2 block 2 JavaTester IIB3 block 3 JavaTester-Constructor CalledFrom the running results, when creating a class object of JavaTester, the compiler attempts to execute the constructor of class JavaTester. But because it has a parent class, after discovering the
super() statement, it switches to executing the constructor of the parent class first.
The key points of the instance initialization block
Let’s review the main points of the instance initialization block.The above is the detailed content of Detailed introduction to instance initialization block (IIB) in Java (code example). For more information, please follow other related articles on the PHP Chinese website!