Why main method is marked as public?

前端 未结 9 1176
情书的邮戳
情书的邮戳 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:47

    Because that is what is known as the "entry point" and if it is private, your program will not be able to run.

    0 讨论(0)
  • 2020-12-10 08:47

    Public - main method is called by JVM to run the method which is outside the scope of project therefore the access specifier has to be public to permit call from anywhere outside the application.

    From coderanch

    0 讨论(0)
  • 2020-12-10 08:48

    The initialization software that starts your program must be able to see main so that it can call it.

    0 讨论(0)
  • 2020-12-10 08:48

    Yes the standards say so that main method should be public in Java. But it's not only for Java. However even for C#, main must be public.

    So just think about it in real world scenarios.

    E.g. you want to enter your room, but you should enter your home main door first (given room is inside the house...and no other means to enter the house)

    Main door is the only publicly available access point.

    0 讨论(0)
  • 2020-12-10 08:58

    I believe, the rational behind enforcing main as public is more to do with the language specification – rather than whether something could be achieved or not.

    Refer: http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.1.4

    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. Therefore, either of the following declarations is acceptable:

    Java uses JNI launch java application will never have any issue in calling a private main – but this is more like jail-brake (like another jail-brake, where reflection API let you access private method) and definitely not in spirit of java specification.


    If I remember till JDK 1.3 – it was not mandatory from developer’s perspective. i.e. even a private main was being accepted by JRE. Though, it was not inline with JLS 1.3.

    I tried searching JLS 1.3 for a reference, but could not get a link. But I found that it was reported as a bug by developers across world:

    Please refer: http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4155575

    So, a fix was made in subsequent version to enforce rule stated by JLS.

    Now, the point is why JLS writer enforced this rule at first place – I really have no idea. The only thing I can think of is that – to keep it “obvious” and non-confusing for developers.

    0 讨论(0)
  • 2020-12-10 08:58

    In Java a function or variable in class in unaccessible untill an Object is created from the class, but the 'main' function is supposed to run at startup(without Object initiation) by JVM. So the 'main' is declared public as well as static so that it can be accessed outside class & without even an Object is created.

    0 讨论(0)
提交回复
热议问题