


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

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



Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.
