Java-Execution of Static Block and Constructor

Execution of Static Block and Constructor

The static blocks execute when the class is loaded, and the non-static blocks(Constructor, methods) execute when each object is created.

Program

Program Source

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

public class Javaapp {

    public static void main(String[] args) {
        
        Data obj = new Data();
    }
}

Leave a Comment