Java-Static Block

Static Block

Sometimes, we may wish to assign initialize values to a set of static variables when a class is loaded into memory. In such situations, we shall define and use a block of statements in the form of a static initialization block. Static block is a special type of blocks are blocks defined within the body of a class using the static keyword. You can declare a static block that gets executed exactly once. The static block are called before the main( ) method.

Program

31

Program Source

public class Javaapp {

    static {
        
        System.out.println("Static block initialized");
    }
    
    public static void main(String[] args) {
       
        System.out.println("Main block executed");
    }
}

Leave a Comment