我的问题是,在Java中我们不能在main函数之外编写输出语句吗?如果我用{ }括号将其括起来,那么就不会出现错误,但如果直接写,则会出现错误。为什么会这样呢?
public class abc
{
int a=3;
int b=0;
System.out.println("this statement gives error"); //Error!!
{System.out.println("this works fine");}
public static void main(String args[]) {
System.out.println("main");
abc t=new abc();
}
}
我尝试在主函数中编写它,它可以工作。为什么没有方法就不能工作呢?
{...}
语法实际上与将其放在类的构造函数中是相同的。 - MirroredFate