Hello world works but then gets error that there's no main?

前端 未结 6 611
星月不相逢
星月不相逢 2020-12-09 09:57

I have the following simple hello world in Java:

class A {
    static {
        System.out.println(\"Hello world\");
    }
}

It works as ex

6条回答
  •  情书的邮戳
    2020-12-09 10:43

    It works as expected, but oddly: No. class gets loaded. And when a class is loaded, its static blocks are executed.

    Why you get Exception in thread "main" java.lang.NoSuchMethodError: main

    Because in first case you dont have a main method and in second case signature is not correct.

    change it to public static void main(String a[])

提交回复
热议问题