Why main method is marked as public?

前端 未结 9 1185
情书的邮戳
情书的邮戳 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 08:59

    When the code is executed, a JVM will be created and that will act as a container for the code we are trying to execute. Declaring this method as public allows the JVM to start the code execution. If method is private, JVM wont be able to call it. There are ways to call a private method as well (eg by using Reflection), but that is not a standard way to do things.

提交回复
热议问题