Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How Static Initializer block works in Java through a simple program?.


Program to demonstrate working of Static Initializer block in Java.

package com.hubberspot.code;

public class StaticInitializationTest {

 // static instance variable
 public static int x = 5;

 // static initialization block
 // it gets executed as soon as 
 // class loads
 static {

  System.out.println("In static initialization block");  
  System.out.println("static instance variable x = " + x);

 }

 public static void main(String[] args) {

  // main method is called by JVM after executing
  // the static initialization block of code
  System.out.println("In the main method");
 }
}



Output of the program :



 
© 2021 Learn Java by Examples Template by Hubberspot