• Home
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • All
  • About
Java | Initializer Blocks
  1. Initializer Blocks

  1. Initializer Blocks
    You can write initializer blocks that will be executed when the class is loaded into memory (static initializer blocks) or when an instance of the class is created (instance initializer blocks).

    • Static initializer blocks:
      class ClassA {
          // static initialization block
          static {
              System.out.println("static initialization block (1)");
          }
      }
      A static initializer block is a block of code placed between curly braces "{}", with the opening brace preceded by the static keyword.

      A class can have multiple static initializer blocks, and they can appear anywhere within the class body.

      When the class is loaded, the static initializer blocks are executed one after the other in the order in which they appear in the class code. In fact, the compiler combines the code from all static initializer blocks (in their order of appearance) into a single "special" method that is invoked when the class is loaded.

      The code inside static initializer blocks is executed only once (at class loading) and is not executed again when instances of the class are created.

    • Instance initializer blocks:
      class ClassA {
          // initializer block
          {
              //...
          }
      }
      An instance initializer block is a block of code placed between curly braces "{}".

      A class can have multiple instance initializer blocks, and they can appear anywhere within the class body.

      When an instance of the class is created, the instance initializer blocks are executed one after the other in the order in which they appear in the class code. In fact, the compiler copies the code from all instance initializer blocks (maintaining their order of appearance) to the beginning of each constructor, provided that the constructor does not call another constructor using the this keyword.

    Example:
    package com.mtitek.initializerBlock;
    
    class ClassA {
        // initializer block
        {
            System.out.println("initializer block (1)");
        }
    
        // static initialization block
        static {
            System.out.println("static initialization block (1)");
        }
    
        // constructor
        ClassA() {
            System.out.println("constructor: ClassA()");
        }
    
        // static initialization block
        static {
            System.out.println("static initialization block (2)");
        }
    
        // initializer block
        {
            System.out.println("initializer block (2)");
        }
    }
    
    public class TestClass {
        public static void main(String[] args) {
            // Loading the ClassA class
            try {
                Class.forName("com.mtitek.initializerBlock.ClassA");
            } catch (ClassNotFoundException e) { }
    
            System.out.println();System.out.println();
    
            // Creating an instance of the ClassA class
            new ClassA();
        }
    }
    Code execution result:
    static initialization block (1)
    static initialization block (2)
    
    initializer block (1)
    initializer block (2)
    constructor: ClassA()
    Initializer blocks (like constructors) are not inherited.
    • Static initializer blocks:
      The static initializer blocks of the superclass are always executed first, followed by the static initializer blocks of the subclass.

    • Instance initializer blocks:
      Since the code of the initializer blocks is copied into each constructor, their execution depends on the execution of the constructors.

    Example:
    package com.mtitek.initializerBlock;
    
    class SuperClass {
        // initializer block
        {
            System.out.println("SuperClass: initializer block (1)");
        }
    
        // static initialization block
        static {
            System.out.println("SuperClass: static initialization block (1)");
        }
    
        // constructor
        SuperClass() {
            System.out.println("constructor: SuperClass()");
        }
    }
    
    class SubClass extends SuperClass {
        // initializer block
        {
            System.out.println("SubClass: initializer block (1)");
        }
    
        // static initialization block
        static {
            System.out.println("SubClass: static initialization block (1)");
        }
    
        // constructor
        SubClass() {
            super();
            System.out.println("constructor: SubClass()");
        }
    }
    
    public class TestClass {
        public static void main(String[] args) {
            // Loading the SubClass class
            try {
                Class.forName("com.mtitek.initializerBlock.SubClass");
            } catch (ClassNotFoundException e) { }
    
            System.out.println();System.out.println();
    
            // Creating an instance of the SubClass class
            new SubClass();
        }
    }
    Code execution result:
    SuperClass: static initialization block (1)
    SubClass: static initialization block (1)
    
    SuperClass: initializer block (1)
    constructor: SuperClass()
    SubClass: initializer block (1)
    constructor: SubClass()
© 2025  mtitek