In this tutorial, you’ll learn how instance variables, methods, constructors, instance blocks, and static blocks work in Java — along with the exact order in which they are executed.

This topic is one of the most commonly asked Java interview questions because it tests your understanding of how the JVM loads classes, creates objects, and initializes members.


1. What Happens When a Class is Loaded?

When a Java program starts, the class loader loads the class into memory. During this process:

  1. All static variables and static blocks are loaded and executed first.
  2. Static members are executed only once when the class is first loaded — before any object is created.

class Demo {
    static int a = 10;

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

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

Output:


Static block executed
Main method executed

Explanation: When the class loads, the static block executes before the main method.


2. Instance Variable and Constructor Execution Order

When you create an object, Java performs these steps in order:

  1. Class is loaded (if not already) → static blocks run once.
  2. Memory is allocated for the object.
  3. Instance variables are initialized.
  4. Instance initialization blocks (if any) are executed.
  5. Constructor runs last.

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

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

    Test() {
        System.out.println("Constructor executed");
    }

    public static void main(String[] args) {
        System.out.println("Main started");
        new Test();
        new Test();
    }
}

Output:


Static block
Main started
Instance block
Constructor executed
Instance block
Constructor executed

Explanation: Static block executes only once at class load time. Each time a new object is created, the instance block executes first, followed by the constructor.


3. Instance Variables and Methods

An instance variable belongs to the object, while a static variable belongs to the class. Instance methods can access both static and instance members, but static methods can access only static members directly.


class Example {
    int instanceVar = 5;
    static int staticVar = 10;

    void show() {
        System.out.println("InstanceVar = " + instanceVar);
        System.out.println("StaticVar = " + staticVar);
    }

    static void display() {
        // System.out.println(instanceVar); // ❌ Error: can't access non-static directly
        System.out.println("StaticVar from static method = " + staticVar);
    }

    public static void main(String[] args) {
        Example ex = new Example();
        ex.show();
        display();
    }
}

Output:


InstanceVar = 5
StaticVar = 10
StaticVar from static method = 10

4. Instance Block with Variable Initialization

Instance blocks are often used to initialize complex variables. They execute before constructors.


class Student {
    String name;
    int id;

    { 
        System.out.println("Instance block called");
        id = 101;
    }

    Student() {
        System.out.println("Constructor called");
        name = "Ashish";
    }

    void show() {
        System.out.println(id + " - " + name);
    }

    public static void main(String[] args) {
        Student s = new Student();
        s.show();
    }
}

Output:


Instance block called
Constructor called
101 - Ashish

5. Execution Order — Static, Instance, Constructor

Let’s combine everything and see the exact execution order:


class A {
    static { System.out.println("Static block of A"); }
    { System.out.println("Instance block of A"); }
    A() { System.out.println("Constructor of A"); }
}

class B extends A {
    static { System.out.println("Static block of B"); }
    { System.out.println("Instance block of B"); }
    B() { System.out.println("Constructor of B"); }

    public static void main(String[] args) {
        System.out.println("Main started");
        new B();
    }
}

Output:


Static block of A
Static block of B
Main started
Instance block of A
Constructor of A
Instance block of B
Constructor of B

Explanation:

  • Static blocks run first when the class loads (parent first, then child).
  • When the object is created, parent’s instance block and constructor run first, then child’s.

6. Final Execution Flow Summary

StepWhat Happens
1Class loaded → Static variables and static blocks executed (once only)
2Object created → Memory allocated for instance variables
3Instance variables initialized (default or explicit)
4Instance block executes
5Constructor executes last

✅ Quick Interview Tip

When asked, “In what order do static and instance elements execute?” — You can confidently answer:

“Static blocks load when the class is loaded. Then, during object creation, instance blocks run before constructors. Hence, the order is: Static → Instance → Constructor.”


Conclusion

You’ve now learned how static and instance elements work in Java, including the complete class loading and object creation sequence.

This understanding helps you answer tricky OOP interview questions involving static vs instance variables, constructors, and execution order confidently.

Next Step: Try creating your own examples combining inheritance, static blocks, and instance blocks — and predict the output before running the program.


💡 Mastering these basics will make OOPs, inheritance, and constructors feel much easier in real-world coding and interviews!