< Prev
Next >



Static Initialization Blocks in Java










Static initialization block


Static initialization block is used to initialize class variables, which are defined with static keyword. You can define a static initialization block in your class and it is automatically called when your class loads, i.e. when you run your program




Note :


Static initialization block precedes with the keyword static.




Static initialization block example


//Static initialization block

class A
{


//Static Initializaton Block
static	
{
System.out.println("Hello from the static block");
}

public static void main(String... ar)
{
}
}


Output-


Hello from the static block

We have defined a static initialization block in class A and it is executed as soon as we run our program.




Static initialization block can only access static variable of a class


Similar to the principle of static method, a static initialization block can only access static variables and static methods defined in the class.
class A
{
static int i;

static	//static initialization block
{
i=10;
System.out.println("A class is loaded");
System.out.println("value of i = "+ i);
}

public static void main(String... ar)
{
}
}


Output-


A class is loaded
value of i = 10





Static initialization block cannot access instance variables


Just like a static method, a static initialization block cannot access instance variables and doing so, gives a compile error.
class A
{
int a=20;

static	//static initialization block
{
System.out.println("A class is loaded");
System.out.println(a);
}

public static void main(String... ar)
{
}

}


Output -


A.java:8: error: non-static variable a cannot be referenced from a static context
System.out.println(a);
                   ^
1 error





Multiple static initialization blocks


You can define multiple static initialization blocks within your class and the order(starting from top) in which they are defined is the order in which they are executed at the time when the class loads.
//multiple static initialization blocks.

class A
{

//first static initialization block
static	
{
System.out.println("First hello from static block");
}

public static void main(String... ar)
{
}

//second static initialization block
static	
{
System.out.println("Second hello from static block");
}


//third static initialization block
static	
{
System.out.println("Third hello from static block");
}

}


Output-


First hello from static block
Second hello from static block
Third hello from static block





Static initialization block in inheritance


When a class which has extended another class, is executed. A static initialization block of a superclass is always executed before the static initialized block of a subclass.
class B
{
static	//static initialization block of A
{
System.out.println("Static block of B");
}
}

class A extends B
{
static	//static initialization block of B
{
System.out.println("Static block of A");
}

public static void main(String... ar)
{
}

}


Output-


Static block of B
Static block of A

Class A extended class B. On execution of class A, the static initialization block of its superclass B is executed before its own static initialization block.



Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter




< Prev
Next >
< Method finalize()
Instance Initialization Block>
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2