Why main method is marked as public?

前端 未结 9 1204
情书的邮戳
情书的邮戳 2020-12-10 08:41

I have a question that why main method is marked as public?

According to an answer on stackoverflow, It is declared as static

9条回答
  •  一个人的身影
    2020-12-10 09:05

    Because the JLS, Section 12.1.4, says so:

    The method main must be declared public, static, and void. It must specify a formal parameter (§8.4.1) whose declared type is array of String.

    If it's not public, then it won't be found; you'll get

    Error: Main method not found in class Main, please define the main method as:
       public static void main(String[] args)
    

提交回复
热议问题