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

Last Updated : 17 Aug, 2026

Java’s main() method is the entry point of every Java program, where the JVM begins execution. If the main() method is missing or its signature is incorrect, the program will compile but won’t run.

  • The JVM looks specifically for a properly defined main() method to start execution.
  • The java.exe launcher initializes the JVM using JNI and invokes the main() method.
  • The main thread created by the JVM is always a non-daemon thread.

Syntax

2056957879
syntax


Example: The most common method to define the main() method is shown in the example below.

Java
class Geeks {
    public static void main(String[] args)
    {
        System.out.println("I am a Geek");
    }
}

Output
I am a Geek

Every word in the public static void main statement has a meaning in the JVM that is described below.

Components of public static void main(String[] args)

1. Public 

public is an access modifier. It allows the Java launcher to access the main() method from outside the class.

  • public provides the required access for the traditional main() method.
  • If the main method is not public, it's access is restricted.
Java
class Geeks
{
    private static void main(String[] args)
    {
        System.out.println("I am a Geek");
    }
}


Output:

ErrorOutput1

2. Static

static indicates that the method belongs to the class rather than an object.

  • The traditional main() method is static so that the Java launcher can invoke it without first creating an object of the class.
  • If you try to run Java code where main is not static, you will get an error.
Java
class Geeks
{
    public void main(String[] args)
    {
        System.out.println("I am a Geek");
    }
}


Output:

OutputError2

3. Void 

void specifies that the main() method does not return a value.

  • A method declared with void cannot return a value using a return statement with an expression.
  • If main method is not void, we will get an error.
Java
class Geeks
{
    public static int main(String[] args)
    {
        System.out.println("I am a Geek");
        return 1;
    }
}


Output:

OutputError3

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.

Java
class Geeks
{
    public static void newmain(String[] args)
    {
        System.out.println("I am a Geek");
    }
}


Output:

OutputError4

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
class Geeks 
{
    public static void main(String[] args)
    {
        for (String elem : args)
            System.out.println(elem);
    }
}


Output:

Output5

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.

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


Comment