Lesson

Block

Opps/Java

Blocks

  • A block is a pair of { } braces.

  • It is used to group a set of statements.

A block can be declared:

  • Inside a class

  • Inside a method

  • Inside a constructor

  • Inside another block


Types of Blocks

  1. Instance Block (Non-Static Block)

  2. Static Block

  3. Local Block

1. Instance Block (Non-Static Block)

  • A block declared inside a class without static keyword

  • Also called Non-Static Block

  • Executes when object is created

  • Runs before constructor

  • Can have multiple instance blocks

  • Execution order → top to bottom (as declared)

  • Can access:

    • Instance variables

    • Static variables

java
class A {

    int a = 10;        // instance variable
    static int b = 20; // static variable

    {
        System.out.println("Instance Block 1");
        System.out.println("a: " + a);
        System.out.println("b: " + b);
    }

    {
        System.out.println("Instance Block 2");
    }
}

class Main {
    public static void main(String[] args) {
        new A();
        new A();
    }
}

Output :

java
Instance Block 1
a: 10
b: 20
Instance Block 2

Runs every time object is created


2. Static Block

  • A block declared with static keyword

  • Executes when class is loaded

  • Runs only once in program life cycle

  • Executes before main() method

  • Can directly access:

    • Static variables ✅

    • Instance variables ❌ (need object)


  • Used for:

    • Initialization logic

    • Database connection setup

    • Configuration loading

java
class A {
    static int b = 20;

    static {
        System.out.println("Static Block in A");
        System.out.println("b: " + b);
    }

    int a = 10;

    {
        System.out.println("Instance Block in A");
    }
}

class Main {
    public static void main(String[] args) {
        new A();
    }
}

Output:

java
Static Block in A
b: 20
Instance Block in A

Static block runs first, then instance block


Important Note (Execution Order)

java
class A {
    static {
        System.out.println("SB-1");
    }

    static int a = 10;

    static {
        System.out.println("SB-2");
        System.out.println(a);
    }
}

Output:

plaintext
SB-1
SB-2
10

Execution depends on declaration order


3. Local Block

  • A block defined inside a method or local context

  • Executes when method is called

  • Not static or instance block

  • Used for:

    • Code grouping

    • Block-level operations

    • Synchronization (advanced use)

java
class A {

    void m1() {

        System.out.println("Before Local Block");

        {
            System.out.println("Inside Local Block");
        }

        System.out.println("After Local Block");
    }
}

class Main {
    public static void main(String[] args) {
        new A().m1();
    }
}

Output:

java
Before Local Block
Inside Local Block
After Local Block

Combined Example

java
class A {

    int a = 10;
    static int b = 20;

    static {
        System.out.println("Static Block in A");
    }

    {
        System.out.println("Instance Block in A");
    }
}

class Main {

    static {
        System.out.println("Static Block in Main");
    }


    public static void main(String[] args) {
        A a1 = new A();
    }
}

Output:

java
Static Block in Main
Static Block in A
Instance Block in A

Block | Java | Softcrayons Tech Solutions