Java Program for Static Block

You are currently viewing Java Program for Static Block

Java program for static block

// Java program to demonstrate example of static block.

import java.util.*;

public class StaticBlock {
    //static variables
    static int a;
    static int b;
    static int c;

    //static block1 
    static {
        System.out.println("I'm in static block 1.");
        a = 10;
    }
    //static block2
    static {
        System.out.println("I'm in static block 2.");
        b = 20;
    }
    //static block3 
    static {
        System.out.println("I'm in static block 3.");
        c = 30;
    }
    public static void main(String[] s) {
        System.out.println("Value of a: " + a);
        System.out.println("Value of a: " + b);
        System.out.println("Value of a: " + c);
    }
}

Output

I'm in static block 1.
I'm in static block 2.
I'm in static block 3.
Value of a: 10
Value of a: 20
Value of a: 30

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply