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

Java main() Method

Java is one of the most popular programming languages in the world. It is widely used to build a variety of applications, ranging from simple desktop programs to complex enterprise systems. One of the most important concepts in Java is the main() method, which is the entry point of a Java application. In this article, we will explain what the main() method is, how it works, and why it is so important.

What is the main() method?

The main() method is a special method in Java that serves as the entry point of a Java application. When you run a Java program, the Java Virtual Machine (JVM) looks for the main() method in the class that you specified and executes it. The main() method is defined as follows:
public static void main(String[] args)

  1. public: This keyword indicates that the main() method can be called from anywhere.
  2. static: This keyword indicates that the main() method is a class-level method that can be called without creating an instance of the class.
  3. void: This keyword indicates that the main() method does not return a value.
  4. main: This is the name of the method.
  5. String[] args: This is an array of strings that are passed to the main() method as command-line arguments.

main() method in Java Class

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

How does the main() method work?

The main() method is executed by the JVM when you run a Java program. The JVM first looks for the class that contains the main() method and loads it into memory. Then, it calls the main() method and passes any command-line arguments to it. When the main() method is executed, it typically initializes any necessary resources, such as variables, objects, or files. Then, it calls other methods or performs any necessary computations. Finally, it terminates the program by either returning from the main() method or by calling the System.exit() method.

Why is the main() method so important?

The main() method is important because it is the entry point of a Java application. Without a main() method, you cannot run a Java program. The main() method also provides a way to pass command-line arguments to a Java program, which can be useful for configuring the program or providing input data. In addition, the main() method is often used to demonstrate how to use a particular Java feature or library. Many Java tutorials and examples use the main() method to show how a program works or how to use a specific API.


Command-line Java Applications

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

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...)