Advertisement



< Prev
Next >



Static Initialization Blocks in Java





This tutorial is solely dedicated to the static initialization block, while the instance initialization block and its technicalities will be explained in the next tutorial.




Static initialization block


Static initialization block can be used to initialize class variables, which are marked with the static keyword. Moreover, a static initialization block is defined within a class, and it is automatically called when its class loads, i.e. when a class containing the static block is executed.




Note :


A static initialization block starts with the static keyword.




Example of static initialization block


//Java - 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

In the coding example, we have defined a static initialization block in class A, and it is executed by executing class A.




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 of a class.

//Java - Static initialization block can only access static variable of a 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



Advertisement




Static initialization block cannot access instance variables


Just like a static method, a static initialization block cannot access instance variables. Doing so gives a compile error.

//Java - Static initialization block cannot access instance variables


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 in which they are defined(starting from the top) is the order in which they are executed when the class loads.

//Java - 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


In inheritance,the static initialization block of a superclass is always executed before the static initialization block of a subclass.

//Java - Static initialization block in inheritance


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 has 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 -





< Prev
Next >
< Method finalize()
Instance Initialization Block>



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement