Interface can not contain static block

:white_check_mark: Example with Static Block and Method:

java

CopyEdit

interface MyInterface {
    static {
        System.out.println("Static block in interface");
    }
    
    static void display() {
        System.out.println("Static method in interface");
    }
}

public class Test implements MyInterface {
    public static void main(String[] args) {
        MyInterface.display(); // βœ… Works fine
    }
}

:backhand_index_pointing_right: Conclusion:

  • :white_check_mark: Interface cannot have a constructor.
  • :white_check_mark: Interface can have static and default methods.
  • :white_check_mark: Interface can have static blocks (executed when the interface is first accessed).

interface can not contain static block But, chatgpt given interface can have static block