Why a class containg a main method doesn't need to be public in Java? -
this question has answer here:
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
Post a Comment