println 语句在块 {} 或函数内部工作,但它在没有函数或块的 class 内部不起作用?

The println statement is working inside a block {} or inside a function but it is not working inside the class without function or block?

public class Main
{
    System.out.println("Error in this line"); //line 1
    public static void main(String[] args) {
        System.out.println("Executed succesfully"); //line 2
    }
}

这里第三行显示错误

illegal start of type

这是什么意思?

任何可执行代码都应该在一个块中。块可以是方法、构造函数、静态块或初始化块。

示例代码:

public class Main {

  // Initializer block
  {
    System.out.println("Hello");
  }

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

  // Constructor
  public Main() {
    System.out.println("Executed succesfully");
  }

  // Method
  public static void main(String args[]) {
    System.out.println("Executed succesfully");
  }

  public static String testMethod() {
    System.out.println("Executed succesfully");
    return "test";
  }

}