Pin It

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 :



Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot