2 Replies Latest reply: Jan 3, 2014 6:34 AM by aksarben RSS

should java program have main method

a8413ea5-fdca-4878-b9fb-70329f00a1b2 Newbie
Currently Being Moderated

should java program have main method

  • 1. Re: should java program have main method
    rp0428 Guru
    Currently Being Moderated
    should java program have main method

    Only the class you want to use to start the Java Virtual Machine needs a 'main' method.


    See Chapter 12 'Execution' of the Java Language Spec


    The Java Virtual Machine starts execution by invoking the method main of some specified class, passing it a single argument, which is an array of strings.

    The Java Tutorials discuss the use of 'main' and have example code:


    In the Java programming language, every application must contain a main method whose signature is:

    public static void main(String[] args)  

    The modifiers public and static can be written in either order (public static or static public), but the convention is to use public static as shown above. You can name the argument anything you want, but most programmers choose "args" or "argv".

    The main method is similar to the main function in C and C++; it's the entry point for your application and will subsequently invoke all the other methods required by your program.

    The main method accepts a single argument: an array of elements of type String.

    public static void main(String[] args

    This array is the mechanism through which the runtime system passes information to your application. For example:

    java MyApp arg1 arg2  

    Each string in the array is called a command-line argument. Command-line arguments let users affect the operation of the application without recompiling it. For example, a sorting program might allow the user to specify that the data be sorted in descending order with this command-line argument:

  • 2. Re: should java program have main method
    aksarben Journeyer
    Currently Being Moderated

    I note this question is still marked as "not answered." Not sure if this is due to inactivity over the holidays, or if there is still confusion. Let me throw in one other wrinkle: Though the JVM requires a main() method to launch a program, your code may or not be responsible for providing that method. In a normal stand alone, command line app, yes, you do have to provide a main method. But there are other common scenarios where you don't. If one of these scenarios applies to you, don't waste time looking through the code for the main method, because you won't find it.


    1. Web Applications. These often run under a servlet that calls/invokes the app to handle individual client requests (e.g., Wicket) . In this scenario, the Web app acts like a "listener." (To confuse things even more, the servlet runs inside a parent container, such as Tomcat). The bottom line is, though, that your application itself won't have a main() method. Some higher level code does.


    2. Parent Controllers. Some frameworks (Spring Batch comes to mind) provide the main method, and call the individual portions of your code to execute steps, tasks, etc. In this scenario, you  again won't see a main() method, because it's "hidden" inside the parent layer.


    There are probably other scenarios as well where the location of the main method isn't obvious. These are just two of the more common ones.


  • Correct Answers - 10 points
  • Helpful Answers - 5 points