Java Blocks Explained: Static Block and Non-Static Block

Java Blocks Explained: Static Block vs Non-Static Block with Examples (2026)

Java Blocks Explained: Static Block vs Non-Static Block with Examples

Blocks are one of the most misunderstood parts of Java for beginners. They have no name, they run automatically, and they show up in interview questions constantly. This guide explains both types — static blocks and non-static (init) blocks — in plain words, with execution order, multiple-block behavior, and the compile errors you will actually see. Every example includes the output.

What are blocks in Java?

A block is a piece of code wrapped in curly braces { } that has no name. It is not a method. You cannot call it. It runs automatically at a specific point in the object or class lifecycle.

Here is what you need to remember about blocks:

  • Blocks are self-executable. You do not call them.
  • Blocks have no name.
  • Blocks are defined inside a class.
  • Blocks are used to run setup code before something else is initialized.

There are two types:

  • Static block — runs once when the class loads.
  • Non-static block (also called init block) — runs every time an object is created.

1. Static Block

A static block is defined with the static keyword. It runs once when the JVM loads the class into memory, before main executes.

Basic syntax

class A {

    static {
        // static block
    }

}

Static block runs when the class is loaded

Even if main is empty, the static block still runs. That is because class loading happens before main is invoked.

class A {

    static {
        System.out.println("static block");
    }

    public static void main(String[] h) {

    }

}

// save and compile
// javac A.java

// run by
// java A

Output is : static block

A class can have multiple static blocks

You can define as many static blocks as you want. They all run once, in the order they appear in the source file.

class A {

    static {
        System.out.println("first static block");
    }

    static {
        System.out.println("second static block");
    }

}

Execution order follows source order

class A {

    static {
        System.out.println("first static block");
    }

    static {
        System.out.println("second static block");
    }

    public static void main(String[] h) {

    }

}

Output is : first static block
Output is : second static block

2. Non-Static Block

A non-static block is defined without the static keyword. It is also called an init block or instance initializer block. It runs every time an object is created, before the constructor body executes.

Basic syntax

class A {

    {
        // non-static block
    }

}

Non-static block runs when an object is created

The block runs only when new A() executes. If you never create an object, the block never runs.

class A {

    {
        System.out.println("non-static block");
    }

    public static void main(String[] h) {
        new A();
    }

}

Output is : non-static block

A class can have multiple non-static blocks

class A {

    {
        System.out.println("first non-static block");
    }

    {
        System.out.println("second non-static block");
    }

}

Execution order follows source order

class A {

    {
        System.out.println("first non-static block");
    }

    {
        System.out.println("second non-static block");
    }

    public static void main(String[] h) {
        new A();
    }

}

Output is : first non-static block
Output is : second non-static block

Why use a non-static block at all?

If you have multiple constructors and they all need the same initialization code, you put that code in a non-static block. The block runs before every constructor, so you do not repeat the code three times.

Static block vs non-static block: the difference

Aspect Static block Non-static block
Keyword static { } { }
When it runs Once, when the class is loaded Every time an object is created
How many times Once per class loader Once per object
Can access static variable Yes Yes
Can access non-static variable No (compile error) Yes
Runs before main and any object creation Constructor body

Common examples and their output

These are the examples that show up in interviews. Each one teaches a different rule.

Example 1: static block reads a static variable declared before it

class A {
    static int i = 10;

    static {
        System.out.println(i);
    }

    public static void main(String[] h) {

    }
}

Output is : 10

The variable is declared before the block. The compiler knows about it. No error.

Example 2: static block reads a static variable declared after it

class A {

    static {
        System.out.println(i);
    }

    static int i = 10;

    public static void main(String[] h) {

    }
}

Output is : Compile time error
/* In case of block, the variable must be
declared first, then used from any block */

This is a forward reference error. Java does not allow a block to read a variable that is declared later in the file, even if the variable is static.

Example 3: static block reads a non-static variable

class A {

    int i = 10;

    static {
        System.out.println(i);
    }

    public static void main(String[] h) {

    }
}

Output is : Compile time error

A static block runs at class-load time, before any object exists. A non-static variable belongs to an object. The compiler rejects this with the error: non-static variable i cannot be referenced from a static context.

Example 4: non-static block reads a non-static variable

class A {

    int i = 10;

    {
        System.out.println(i);
    }

    public static void main(String[] h) {
        new A();
    }
}

Output is : 10

The block runs after the object is created, so i is available. No error.

Example 5: non-static block reads a static variable declared before it

class A {

    static int i = 10;

    {
        System.out.println(i);
    }

    public static void main(String[] h) {
        new A();
    }
}

Output is : 10

A non-static block can always read a static variable. No restriction there.

Example 6: non-static block reads a static variable declared after it

class A {

    {
        System.out.println(i);
    }

    static int i = 10;

    public static void main(String[] h) {
        new A();
    }
}

Output is : 10

This one is different from the static block case. A non-static block runs when an object is created. By that time, all static variables in the class have already been initialized, regardless of source order. So the value is available.

This is the trick that catches most people. The forward reference rule applies differently to static and non-static blocks.

Where to go next

You now understand both types of blocks, their execution order, and the compile errors that come from accessing the wrong kind of variable from the wrong kind of block.

If you remember one thing, remember this: a static block belongs to the class and runs once. A non-static block belongs to the object and runs every time you create one. Everything else follows from that.

If you want to go deeper, try this: combine a static block, a non-static block, and a constructor in one class. Run it. Predict the order before you run it. Then check your answer. The order is: static block, non-static block, constructor. Once you see it print, you will never forget it.

No comments: