why public for main

Eric Blake ebb9@email.byu.edu
Mon Apr 7 13:47:00 GMT 2003

Because JVMS 2.17.1 (and also JLS 12.1.4) require that the entry point 
of any Java program be public static void main(String[]).  The main 
method can optionally be final, synchronized, strictfp, or native 
(although I've never run across a native main method).  Likewise, the 
throws clause (if included) for the main method is arbitrary and has no 

Yes, you can write a main with a different signature (ie. non-public, 
non-static, different return type, or overloaded with different 
parameters), but it can then only be called from your code, rather than 
being the entry point.

If gcj compiles a program with the --main option pointing to a class 
without a public static void main, it should be a compile-time (or at 
least a link-time) error. If gij runs a Java program that does not have 
a public static void main, it should be a runtime error. (If this isn't 
the case, these programs have a bug; file a bug report.)

amroz kamal siddiqui wrote:
> hello friends out there,
>           Iam  a newbie in java, I just have a simple and a very
> annoying ( at least to me ) question.  Why do we use the keyword
> public for the main method ?
> eg.
>     public static void main(String arg[])

This signature intentionally left boring.

Eric Blake             ebb9@email.byu.edu
   BYU student, free software programmer

More information about the Java mailing list