Java main() Method public static void main(String[] args) (original) (raw)
Last Updated : 11 Apr, 2025
**Java’s main() method is the starting point from where the **JVM starts the execution of a Java program. JVM will not execute the code if the program is missing the main method. Hence, it is one of the most important methods of Java, and having a proper understanding of it is very important.
The Java compiler or JVM looks for the main method when it starts executing a Java program. The signature of the main method needs to be in a specific way for the JVM to recognize that method as its entry point. If we change the signature of the method, the program compiles but does not execute.
The execution of the Java program, the ****java.exe,**is called. The Java.exe in turn makes Java Native Interface or JNI calls, and they load the JVM. The java.exe parses the command line, generates a new String array, and invokes the main() method. By default, the main thread is always a non-daemon thread.
**Syntax of main() Method
Syntax of the main() method is always written as:
**Example: The most common method to define the **main() method is shown in the example below.
Java `
// Java Program to demonstrate the // syntax of the main() function class Geeks { public static void main(String[] args) { System.out.println("I am a Geek"); } }
`
Every word in the **public static void main statement has a meaning in the JVM that is described below:
1. Public
It is an **Access modifier, which specifies from where and who can access the method. Making the main() method public makes it globally available. It is made public so that JVM can invoke it from outside the class as it is not present in the current class.
If the main method is not public, it’s access is restricted.
**Example:
Java `
// Java Program to demonstrate the // use of any other access modifier // other than public class Geeks { private static void main(String[] args) { System.out.println("I am a Geek"); } }
`
**Output:
2. Static
It is a **keywordthat is when associated with a method, making it a **class-related method. The _main() method is static so that JVM can invoke it without **instantiating the class. This also saves the unnecessary wastage of memory which would have been used by the object declared only for calling the _main() method by the JVM.
If you try to run Java code where main is not static, you will get an error.
**Example:
Java `
// Java Program to demonstrate the // error occurred when we dont use the // static keyword in the main() method class Geeks { public void main(String[] args) { System.out.println("I am a Geek"); } }
`
**Output:
3. Void
It is a keyword and is used to specify that a method does not return anything. As the main() method does not return anything, its return type is void. As soon as the main() method terminates, the Java program terminates too. Hence, it doesn’t make any sense to return from the main() method as JVM can’t do anything with its return value of it.
If main method is not void, we will get an error.
**Example:
Java `
// Java Program to demonstrate the // error occurred when we dont use the // void return type in the main() method class Geeks { public static int main(String[] args) { System.out.println("I am a Geek"); return 1; } }
`
**Output:
4. main
It is the **name of the Java main method. It is the **identifier that the JVM looks for as the **starting point of the Java program. It’s not a keyword.
If we change the name while initiating main method, we will get an error.
**Example:
Java `
// Java Program to demonstrate the // error occurred when we name the // main() method as newmain. class Geeks { public static void newmain(String[] args) { System.out.println("I am a Geek"); } }
`
**Output:
5. String[] args
It **stores Java command-line arguments and is an array of type **java.lang.String class. Here, the name of the String array is _args but it is not fixed and the user can use any name in place of it.
**Example: Execution Process of String[]
Java `
// Java Program to demonstrate // the working of String[] args // in the main() method class Geeks { public static void main(String[] args) { for (String elem : args) System.out.println(elem); } }
`
**Output:
Apart from the above-mentioned signature of main, you could use **public static void main(String args[]) or **public static void main(String… args) to call the main function in Java. The main method is called if its formal parameter matches that of an array of Strings.
Overloading main() Method in Java
Overloading the main() method is possible in Java, meaning we can create any number of main() methods in a program.
To overload the main() method in Java, we need to create the main() method with different parameters.
**Example: Overloading main method in Java.
Java `
public class Main {
public static void main(String[] args) { if (args.length == 0) { System.out.println("Running main() with no arguments"); } else if (args.length == 1) { try { int value = Integer.parseInt(args[0]); main(value); } catch (NumberFormatException e) { main(args[0]); } } else { // Handle more arguments as needed } }
public static void main(int value) { System.out.println("Running main() with integer argument: " + value); }
public static void main(String message) { System.out.println("Running main() with string argument: " + message); } }
`
Output
Running main() with no arguments