J2EEOnline J2EEOnline

Java Programs  «Prev  Next»
Lesson 4Java main() method
ObjectiveInteraction between the Java Interpreter and the main() Method

Interaction between the Java Interpreter and the main() Method

Command-line Java applications are the simplest of all Java programs. Although they require very little overhead, command-line applications do require a main() method. The execution of an application begins with this method.
You already know that the Java interpreter is used to run applications. When an application is executed within the Java interpreter, the interpreter calls the main() method and executes the code within it. The following figure shows how the main() method is called by the Java interpreter to execute an application.

The main method is called by the Java interpreter

Skeletal Application

The following code shows a bare-bones command-line Java application, which should give you an idea as to how simple they can be:

class Skeleton{
  public static void main(String args[]){
  1. The Skeleton application class definition
  2. The main() method definition
  3. The close of the main() method definition
  4. The close of the application class definition

Java Main Method

The public keyword is an access modifier.
When the public keyword is applied to the main() method it means that the method is visible everywhere.
The main() method in the Skeleton application is defined as static, which means that the member is associated with the class itself and not a specific instance of the class. When you run this application in the Java interpreter, the main() method is called but there is no code to execute. So, main() returns and the interpreter terminates the application.

main() method

The main() method only appears in Java applications, and not in applets.
Applets have an init() method that is called to perform applet initialization, but an applet does not execute solely through the init() method in the same way that an application executes through the main() method.

As you can see, the main() method is responsible for the start and finish of an application. The following figure shows how an application ends when the main() method finishes executing.

How an application is terminated when the main() method finishes executing

The first requirement in creating an executable Java application is to create a class with a method whose signature (name and method arguments) match the main method, defined as follows:
public class HelloExam {
  public static void main(String args[]) {
    System.out.println("Hello exam");

This main method should comply with the following rules:
  1. The method must be marked as a public method.
  2. The method must be marked as a static method.
  3. The name of the method must be main.
  4. The return type of this method must be void.
  5. The method must accept a method argument of a String array or a variable argument of type String.

It is valid to define the method parameter passed to the main method as a variable argument (varargs) of type String:
public static void main(String... args)
To define a variable argument variable, the ellipsis (...) should follow the type of the variable and not the variable itself (a mistake made by lot of new programmers):
The following method declaration will not compile.
public static void main(String args...)