Java main() Method

Last Updated : 29 Sept 2026

The main() method is the entry point of a standalone Java application, where the Java Virtual Machine (JVM) begins program execution. It follows a specific method signature that allows the JVM to identify and invoke it when a Java program is run.

In this chapter, you will learn about the main() method in Java, its syntax, and the purpose of the keywords used in its declaration.

What is main() Method in Java?

The main() method is the starting point of execution for a standalone Java application. The JVM looks for a method with the required signature to start executing the program.

The standard syntax of the main() method is:

public static void main(String[] args)
Java Main Method

Example of Java main() Method

In the following example, we define the main() method with the standard signature and print a message to the console.

Java

public class Main {  
    public static void main(String[] args) {  
        System.out.println("Hello, World!");  
    }  
}  
Compile and Run

Output:

Hello, World!

Understanding the main() Method

The standard main() method declaration contains the public, static, and void keywords, followed by the method name and a String[] parameter.

1. public

The public keyword is an access modifier that makes the main() method accessible to the JVM. The JVM must be able to access the method to start program execution.

If the main() method is declared as private, protected, or with default access, the JVM cannot use it as the entry point of the program.

Example

In the following example, the main() method is declared as private, so the JVM cannot recognize it as the program's entry point.

Java

public class Main {
    private static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Output:

Error: Main method not found in class Main, please define the main method as:
public static void main(String[] args)

2. static

The static keyword makes the main() method a class method, so the JVM can invoke it without creating an object of the class. This is why the main() method is declared as static.

If the main() method is declared without static, the JVM cannot invoke it as the entry point of a standalone Java application.

Example

In the following example, the main() method is declared without the static keyword, so the JVM cannot use it as the entry point.

Java

public class Main {
    public void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Output:

Error: Main method is not static in class Main

3. void

In Java, every method has a return type. The void keyword acknowledges the compiler that the main() method does not return any value. If we specify another return type like int instead of void, we get an error. For example, consider the following Java program.

Java

Output:

Error: Main method must return a value of type void in class Main, please 
define the main method as:
   public static void main(String[] args)

4. main()

It is a default signature that is predefined in the JVM. It is called by the JVM to execute a program line by line and end the execution after completion of this method. We can also overload the main() method.

If we change the name while initiating the main method, we will get an error. For example, consider the following Java program.

Java

Output:

Error: Main method not found in class Main, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

5. String args[]

The main() method also accepts some data from the user. It agrees with a group of strings, which is called a string array. It is used to hold the command line arguments in the form of string values.

Here, agrs[] is the array name, and it is of String type. It means that it can store a group of strings. Remember, this array can also store a group of numbers, but in the form of a string only. Values passed to the main() method are called arguments. These arguments are stored in the args[] array, so the name args[] is generally used for it.

Java

Compile and Run

Output:

Java Main Method

What happens if the main() method is written without String args[]?

The program compiles, but does not run, because the JVM does not recognize the main() method. Remember, JVM always looks for the main() method with a string type array as a parameter.

Execution Process

First, JVM executes the static block, then it executes static methods, and then it creates the object needed by the program. Finally, it executes the instance methods. JVM executes a static block on the highest priority basis. It means JVM first goes to the static block even before it looks for the main() method in the program.

Java

Compile and Run

Output:

Static block
Static method

We observe that the JVM first executes the static block, if it is present in the program. After that, it searches for the main() method. If the main() method is not found, it gives an error.

Example

A program that does not have the main() method gives an error at run time.

Java

Output:

Error: Main method not found in class Main, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

So, the main() method should always be written as:

We can interchange public and static and write it as follows:

We can also use a different name for the String type array and write it as:

Different ways of writing the main() method are:

String...args: It allows the method to accept zero or multiple arguments. There should be exactly three dots between String and array; otherwise, it gives an error.

Java Program Without main() Method

It is not possible to directly execute a standalone Java program without a main method from JDK 7 onwards. The Java Virtual Machine (JVM) specifically looks for a public static void main(String[] args) method as the entry point for program execution. If this method is not found, the JVM will report an error indicating its absence.

Prior to JDK 7, it was possible to achieve some execution without a main method by placing code within a static initialization block. These blocks execute when the class is loaded, before the main() method would typically be invoked. 

However, even in such cases, the program would still terminate with a "main() method not found" error after the static block completed execution, unless System.exit(0) was explicitly called within the static block to prevent this.

Java

Output:

Main.java:1: error: package javafx.application does not exist
abstract class Main extends javafx.application.Application  

Note: If you want to execute the above Java program, run it on JDK 7 or prior versions.

Overloading of main() Method

We can also overload the main() method. We can define any number of main() methods in the class, but the method signature must be different.

Java

Compile and Run

Output:

The main() method invoked
6

Next TopicJava Tutorial