


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.
- Execute the instance initialization block of the parent class
- Execute the constructor of the parent class
- Execute the instance initialization block of the current class
- Execute the constructor of the current class
The key points of the instance initialization block
Let’s review the main points of the instance initialization block.- Instance initialization blocks are run every time a new instance is created
- Initialization blocks are run in the order they appear in the class
- If there is a parent class, the parent class will be instantiated first, then the instance initialization block of the current class will be called, and finally the constructor of the current class will be called.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor