5 Replies Latest reply: Jan 22, 2004 12:11 AM by 807582 RSS

    private main method

    807582
      if i write private static void main(String s[]) then it also behave as normal as public static void main(String s[]) How it is possible while i m declaring main method as private and now how JVM interpreter can access this method from the class while it is private now..??
        • 1. Re: private main method
          807582
          This is a bit strange indeed; I ripped the following clause right from the JLS (paragraph 12.1.4)

          The method main must be declared public, static, and void. It must accept a single argument that
          is an array of strings.


          So a private static void main(String[] args) should fail according to the 'law'.

          kind regards,

          Jos
          • 2. Re: private main method
            807582
            but it compiles as well as runs correctly its really a strenge !!!!!
            • 3. Re: private main method
              800560
              What version of java are you using? Prior to 1.4 (or may be 1.3) java allowed public or private to be specified for the main() method and it treated it as public.
              • 4. Re: private main method
                807582


                Both the JLS and the JVM specs require that the main method to be public (note however that you are allowed to have a private, static, void method that takes a single String-array argument, but it should not be considered an application entry-point).

                This was a bug introduced into Sun's VMs with early 1.2 releases; Originally there was no intention to fix it, but eventually they decided to do so, and it was fixed for 1.4.

                If you do not declare your main methods public, static and void, you will find that your applications stop working one day when you switch Java versions, or when you roll the code out to clients using a different version to that which you developed and tested the apps on.


                • 5. Re: private main method
                  807582
                  corrently i am working on 1.4 version and if i write
                  public class U
                  {
                       private static void main(String[] args)
                       {
                            System.out.println("Hello World!");
                       }
                  }
                  it compiles and when i run this program it shows "Main method not public" I think bug is fixed now.