Why a class containg a main method doesn't need to be public in Java? -


i wrote following code

    class hello //note class not public   {         public static void main(string args[]) {
system.out.println("hello"); } }

so, when run it, runs fine , prints output "hello".

however, if jvm spec mandates main method should public since "it can't see main otherwise", shouldn't apply class well? if jvm "can't see" hello.main() when not declared public, how able see class itself.

is there explanation other "because specification says so"?

and if jvm able see classes , methods "security/visibility enforcer" why main method needs declared public.

just kicks, demo private classes can hold main:

class outer {     private static class inner {         public static void main(string[] args) {             system.out.println("hello inner!");         }     } } 

compiles , runs fine command line:

c:\junk>javac outer.java
c:\junk>java outer$inner
hello inner!

c:\junk>


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -