


What\'s the Initialization Order of Static and Instance Blocks in Java Inheritance?
Initialization Sequence of Static and Instance Blocks in Java
When working with multiple classes in Java, understanding the order of execution for static and instance initializer blocks becomes crucial. While it's known that within a class, these blocks run in order of appearance, there remains uncertainty regarding their behavior across classes.
To demonstrate this behavior, consider the following code:
package pkg; public class LoadTest { public static void main(String[] args) { System.out.println("START"); new Child(); System.out.println("END"); } } class Parent extends Grandparent { // Instance init block { System.out.println("instance - parent"); } // Constructor public Parent() { System.out.println("constructor - parent"); } // Static init block static { System.out.println("static - parent"); } } class Grandparent { // Static init block static { System.out.println("static - grandparent"); } // Instance init block { System.out.println("instance - grandparent"); } // Constructor public Grandparent() { System.out.println("constructor - grandparent"); } } class Child extends Parent { // Constructor public Child() { System.out.println("constructor - child"); } // Static init block static { System.out.println("static - child"); } // Instance init block { System.out.println("instance - child"); } }
Expectedly, the output of this code aligns with the assumption that static blocks execute before instance blocks:
START static - grandparent static - parent static - child instance - grandparent constructor - grandparent instance - parent constructor - parent instance - child constructor - child END
However, this observation alone does not clarify the initialization order between parents and children classes. To further explore this aspect, consider adding the following unused class to the code:
class IAmAClassThatIsNeverUsed { // Constructor public IAmAClassThatIsNeverUsed() { System.out.println("constructor - IAACTINU"); } // Instance init block { System.out.println("instance - IAACTINU"); } // Static init block static { System.out.println("static - IAACTINU"); } }
Remarkably, the modified code still produces the same output as the original code. This implies that the static and instance initializer blocks execute in the following sequence:
- Static initializer blocks of all classes and interfaces are executed in a breadth-first top-to-bottom order, irrespective of their dependency relationships.
- Once all static initializer blocks have executed, instance initializer blocks and constructors of all classes are executed in the order in which the classes are initialized in the program.
This behavior aligns with the Java Language Specification (JLS), which provides detailed explanations in sections 12.4 and 12.5.
The above is the detailed content of What\'s the Initialization Order of Static and Instance Blocks in Java Inheritance?. 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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Start Spring using IntelliJIDEAUltimate version...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...
