Search Logic Blocks

Friday, August 14, 2020

Java: Static Initialization Block

What is a Block?

So far, we have written so many programs and in every program we have different kinds of blocks - class block, method block, loop block etc. Any code between opening parentheses and closing parentheses is called a block. And the variables declared in the block can not be accessed outside from the block. For ex:

class [class name] {

}

for(.....) {

}

We can have a block without any name like below :

{
    int x = 5;
}

And the variable x is not accessible outside from the block. Here is the example code -
public class Block {
public static void main(String args[]) {
int x = 10;
String str = "Block Example";
System.out.println("Value of x is : " + x);
System.out.println("This is : " + str);
{
int y = x + 5;
System.out.println("This is inside the block");
}
// System.out.println("Value of y is : " + y);
}
}
In the above code, commented code shows that the variable is declared in the block, so it can not be accessed outside from the block. Here is the output of above code -

Value of x is : 10
This is : Block Example
This is inside the block

What is an Initialization Block?

Initialization block, is the block of code used when initializing the class variables at the start of the class declaration. Mostly initialization block is used to assign the default values to class variables. The initialization block can have any valid Java statement. Here is the example of initialization block -
public class InitBlock {
int x;
int y;
static int z;

{
System.out.println("Initialization Block");
x = 10;
y = 15 + x;
z = 20;
System.out.println("Value of x is: " + x);
System.out.println("Value of y is: " + y);
}
}
public class InitBlockDemo {
public static void main(String args[]) {
InitBlock ib = new InitBlock();
System.out.println("value of z is: " + InitBlock.z);
}
}
In the above example, initialization block has the print statement. When the instance is created, the statements in initialization block are executed. Here is the output -

Initialization Block
Value of x is: 10
Value of y is: 25
value of z is: 20

What is Static Initialization Block?

We have already discussed about Static Variables, Static Methods, Static Inner Classes. We already know, that to access the static variables, methods and inner classes, there is no need to create the instance of the class. They can be accessed directly through class itself. And, we also know that the static methods and the static inner classes can access only the other static members of enclosing class, not non-static members.

Static initialization block is the initialization block which is used to initialize only the static class variables. It can not access non-static variables. Here is the conversion of above example into static initialization block example.
public class StaticInitBlock {
int x;
int y;
static int z;

static {
System.out.println("Static Initialization Block");
// x = 10;
// y = 15 + x;
z = 20;
// System.out.println("Value of x is: " + x);
// System.out.println("Value of y is: " + y);
System.out.println("Values x and y are not accessible in static block");
}
}
public class StaticInitBlockDemo {
public static void main(String args[]) {
System.out.println("value of z is: " + StaticInitBlock.z);
}
}
Here, the commented code in the above example shows that the non-static variables can not be initialized in the static initialization block. Here is the output -

Static Initialization Block
Values x and y are not accessible in static block
value of z is: 20

Here is the link for example code at Github.

No comments: