


What is the execution order of static code blocks, construction code blocks, and construction methods?
The execution order is: first execute the "static code block", then execute the "constructed code block", and finally execute the "constructed code block". Static code blocks are at the class level, while structural code blocks and construction methods are at the instance level, so the static code blocks are executed first; and because the structural code blocks are independent and must rely on a carrier to run, the structural code blocks need to be placed in the structure before method.
The operating environment of this tutorial: windows7 system, java8 version, DELL G3 computer.
Construction code block, static code block and construction method are three different code blocks in the class, so what are the differences between them?
1. Introduction to three types of code blocks
Static code block: declared with staitc, executed when jvm loads the class, only executed once
Constructed code block: directly used in the class {} definition, executed every time an object is created.
Execution order priority: static block, main(), construction block, construction method.
class A { //构造代码块 { System.out.println("构造代码块A"); } //静态代码块 static { System.out.println("静态代码块A"); } //构造方法 public A() { System.out.println("构造方法A"); } }
The execution order of the second and third parties
In order to clarify the execution order of the three, we instantiate class A and test single instances and multiple instances respectively.
2.1 Single instance
class Demo { public static void main(String[] args) { new A(); } }
2.2 Multiple instances
class Demo { public static void main(String[] args) { new A(); new A(); new A(); } }
3. The order in the inheritance system
class B extends A { //构造代码块 { System.out.println("构造代码块B"); } //静态代码块 static { System.out.println("静态代码块B"); } //构造方法 public B() { System.out.println("构造方法B"); } } class Demo { public static void main(String[] args) { new B(); } }
4. Summary
1. In the process of creating an object, the execution order of the three is: Static code block --> Construction code block --> Construction method;
1. Static code block: It is performed during the third step of initialization of the class loading process. Its main purpose is Is to assign an initial value to the class variable.
2. Construction code block: It is independent and must be attached to a carrier to run. Java will place the construction code block in front of each construction method to instantiate some common instance variables and reduce the amount of code.
3.Construction method: used to instantiate variables.1 is at the class level, 2 and 3 are at the instance level, so naturally 1 takes precedence over 23.
Let’s understand one thing: the active use of a subclass will lead to the active use of its parent class, so although the subclass is instantiated, it will also lead to the initialization and instantiation of the parent class, and it is optimal Executed in subclasses.
2. Every time an object is created, the construction code block and construction method will be executed once; no matter how many objects are created, the static code block will only be executed once when the first object is created;
3. When creating a subclass object, the static code block of the subclass is executed after the static code block of the parent class, but takes precedence over the construction code block and construction method of the parent class;
4. When creating a subclass object, The construction code block of the subclass is executed after the construction method of the parent class.
Recommended related video tutorials: Java video tutorial
The above is the detailed content of What is the execution order of static code blocks, construction code blocks, and construction methods?. 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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Spring Boot simplifies the creation of robust, scalable, and production-ready Java applications, revolutionizing Java development. Its "convention over configuration" approach, inherent to the Spring ecosystem, minimizes manual setup, allo
