Back to The main Method
Q1Question 1 of 5

Why is the main method public, static, and void?

Key Concepts
  • public: the JVM must access it from outside the class – if it were private, the JVM could not call it
  • static: the JVM calls it without creating an object – no constructor runs before main
  • void: there is no caller to return a value to – the JVM uses System.exit(code) for exit status
  • String[] args: receives command-line arguments passed when running the program
  • The exact signature is: public static void main(String[] args) – any change and the JVM will not find it
  • Java 21+ allows unnamed main methods and instance main for simpler programs (JEP 463)

Definition

public so the JVM can access it from outside, static so no object is needed, void because there is no caller to return to. String[] args receives command-line arguments.

Each keyword in public static void main(String[] args) has a specific reason:

  • public – the JVM invokes main from outside your class. If it were private or default, the JVM could not access it.
  • static – the JVM calls main without creating an instance of the class. There is no object yet when the program starts.
  • void – main does not return a value to any caller. The JVM uses System.exit(int) if the program needs to report an exit code.
  • String[] args – the array of command-line arguments. java MyApp arg1 arg2 passes ["arg1", "arg2"] to main.

The main method is the front door of a bank branch. It must be public so anyone (the JVM) can walk in. It must be static because the branch operates before any customer account (object) is created. It returns void because the person walking in does not expect a return receipt – they start a process. The args are like documents the visitor brings through the door.

Understanding the Concepts

What is the exact signature?

The JVM looks for exactly this method to start your program:

JAVA
public static void main(String[] args) {
    // entry point
}

If any part is different – private instead of public, non-static, returns int instead of void, or different parameter type – the JVM cannot find the entry point and the program fails with Main method not found.

Why public?

The JVM launcher is external to your class. It needs to call main from outside. If main were private, protected, or package-private, the JVM could not invoke it.

JAVA
// This compiles but the JVM cannot find it as an entry point
private static void main(String[] args) {  // NOT the entry point!
    System.out.println("This won't run via java command");
}

Why static?

When the JVM starts, no objects exist yet. The JVM loads the class and calls main directly on the class itself, without calling any constructor.

JAVA
public class BankApp {
    private String bankName;  // instance field

    public static void main(String[] args) {
        // 'this' does not exist here – main is static
        // System.out.println(bankName);  // COMPILE ERROR: non-static field

        // To use instance fields, create an object first
        BankApp app = new BankApp();
        app.bankName = "National Bank of India";
        System.out.println(app.bankName);
    }
}

Why void?

In C/C++, main returns an int exit code. In Java, main returns void. The exit code is communicated through System.exit(int):

JAVA
public static void main(String[] args) {
    try {
        processTransactions();
        System.exit(0);   // success
    } catch (Exception e) {
        System.err.println("Fatal: " + e.getMessage());
        System.exit(1);   // failure
    }
}

If you do not call System.exit(), the JVM exits with code 0 when main returns normally, or a non-zero code on uncaught exceptions.

How do command-line arguments work?

JAVA
public class TransferApp {
    public static void main(String[] args) {
        if (args.length < 3) {
            System.out.println("Usage: java TransferApp <from> <to> <amount>");
            System.exit(1);
        }
        String from = args[0];     // "SAV2001"
        String to = args[1];       // "CUR3001"
        String amount = args[2];   // "50000"

        System.out.println("Transfer " + amount + " from " + from + " to " + to);
    }
}
BASH
java TransferApp SAV2001 CUR3001 50000
# Output: Transfer 50000 from SAV2001 to CUR3001

Args are always Strings. Parse them with Integer.parseInt() or BigDecimal for numeric values.

What about Java 21+ simplified main (JEP 463)?

Java 21 preview allows simpler entry points for small programs:

JAVA
// Java 21+: unnamed class, instance main, no String[] args required
void main() {
    System.out.println("Hello from simplified main");
}

This is for learning and scripting. Production applications still use the classic signature.

What are common mistakes?

  • Spelling main wrong. Main, mian, main() without args – all fail silently (no compile error, just "Main method not found" at runtime).
  • Forgetting static. An instance main method compiles but is not the JVM entry point (before Java 21).
  • Returning int. public static int main(String[] args) is not the entry point. Use void + System.exit().
  • Accessing instance fields from main. Main is static – create an object first.

What should a senior engineer know?

  • Spring Boot's main just calls SpringApplication.run(). The real entry point sets up the application context: SpringApplication.run(BankApp.class, args);
  • JUnit does not use main. Test runner invokes test methods via reflection. But you can add a main for manual testing.
  • The JVM entry point is configurable. The Main-Class attribute in MANIFEST.MF specifies which class has the main method in a jar.
  • Multiple classes can have main. Each is a valid entry point. You choose which one to run via java ClassName or the manifest.

Visual Overview

DIAGRAM
  java TransferApp SAV2001 CUR3001 50000
    │
    ▼
  JVM starts → loads TransferApp.class
    │
    ▼
  Looks for: public static void main(String[] args)
    │
    ├─ Found? → calls main(args=["SAV2001","CUR3001","50000"])
    │
    └─ Not found? → "Main method not found in class TransferApp"

  Why each keyword:
    public  → JVM calls from outside the class
    static  → no object exists yet at startup
    void    → no caller to return to (use System.exit for code)
    String[]→ command-line arguments

Key Takeaway

The main method signature public static void main(String[] args) is required because each keyword serves a specific purpose. Public lets the JVM access it from outside the class. Static lets it be called without creating an object, since no objects exist when the program starts. Void means it does not return a value to any caller – use System.exit() for exit codes. And String[] args receives the command-line arguments.

Change any part of this signature and the JVM will not find the entry point. The program compiles but fails at runtime with "Main method not found." This is a common trap for beginners.

One sentence to remember: main is public so the JVM can reach it, static because no object exists yet, void because there is no caller, and String[] args carries command-line input.

---

Share
Use ← → arrow keys to navigate