Static Blocks in Java


The static block is a set of instructions that is run only once when a class is loaded into memory. A static block is also called a static initialization block. This is because it is an option for initializing or setting up the class at run-time.

Source Code

//Static Blocks in Java
class BlockTest
{
    static {
        System.out.println("BlockTest-1");
    }
    static {
        System.out.println("BlockTest-2");
    }
}
public class staticBlocks {
    static {
        System.out.println("Block-1");
    }
    public static void main(String[] args) {
        BlockTest o =new BlockTest();
        System.out.println("Main");
    }
    static {
        System.out.println("Block-2");
    }
}
 

Output

Block-1
Block-2
BlockTest-1
BlockTest-2
Main
To download raw file Click Here
`

Basic Programs