Static block is used for initializing the static variables.This block gets executed when the class is loaded in the memory. A class can have multiple Static blocks, which will execute in the same sequence in which they have been written into the program.
Example:
Here both the static variables were initialized before we accessed them in the main method.
class Example{
static int n;
static String str;
static{
n = 97;
str = "Static keyword in Java";
}
public static void main(String args[])
{
System.out.println("Value of num: "+n);
System.out.println("Value of mystr: "+str);
}
}