class A{System.out.println("start");Thread.sleep(1000);System.out.println("end");}
3 回答

莫回无
TA贡献1865条经验 获得超7个赞
public class TestStaticAera {
static{
System.out.println("start");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("end");
}
}
类里面可以存在静态块,在块里面可以写语句,类似上面。但是缺少了main方法,在执行的时候就会提示
java.lang.NoSuchMethodError: main
Exception in thread "main" 。
添加main方法,就不会报错了。

月关宝盒
TA贡献1772条经验 获得超5个赞
1.所有这些语句都必须在方法里面写
2.Thread.sleep()需要捕捉异常
try
{
Thread.sleep(1000);
} catch(Exception e){}
添加回答
举报
0/150
提交
取消