← Back to Home

main() Method Explained

The main() method is the entry point of a Java application. The JVM starts program execution from this method, which makes it one of the most important concepts in Core Java and a frequent interview topic.

Standard Syntax of main() Method

public static void main(String[] args)

This exact signature is recognized by the JVM.

Breakdown of Each Keyword

1. public

  • Makes the method accessible to the JVM
  • JVM is outside the class, so it must be public

Why it matters:

If main() is not public, the JVM cannot invoke it.

2. static

  • Allows JVM to call the method without creating an object
  • Execution starts without instantiating the class

Why it matters:

JVM does not know how to create objects automatically.

Example:

public static void main(String[] args) {
    // JVM calls this directly
}
          

3. void

  • Indicates the method does not return any value
  • JVM does not expect a return value from main()

Why it matters:

The JVM simply executes instructions; it does not use returned data.

4. main

  • Special method name recognized by JVM
  • Case-sensitive (main, not Main or MAIN)

Why it matters:

If the method name is incorrect, JVM will not find the entry point.

5. String[] args

  • Command-line arguments
  • Passed to the program at runtime
  • Stored as an array of String

Example:

java Test hello world

args[0] = "hello"
args[1] = "world"
          

Full Example Program

public class TestApp {
    public static void main(String[] args) {
        System.out.println("Application started");
        System.out.println("Arguments count: " + args.length);
    }
}
          

Can We Change the main() Method Signature?

Valid Variations (Allowed)

static public void main(String[] args)
public static void main(String... args)
public static void main(String args[])
          

All above are valid, because the signature remains logically the same.

Invalid Variations (Not Allowed)

private static void main(String[] args)
public void main(String[] args)
public static int main(String[] args)
          

Reason:

JVM will not recognize these as the entry point.

Overloading the main() Method

  • Yes, main() can be overloaded
  • JVM always calls the original signature only

Example:

public class Demo {
    public static void main(String[] args) {
        main(10);
    }
    public static void main(int x) {
        System.out.println("Overloaded main");
    }
}
          

Execution Flow with main()

  1. JVM loads the class
  2. JVM looks for public static void main(String[] args)
  3. JVM executes statements inside main()
  4. Program terminates after execution completes

Common Beginner Mistakes

  • Missing static keyword
  • Incorrect method name
  • Wrong parameter type
  • Making main() non-public
  • Assuming return value is used

Interview-Ready Answers

Short Answer

The main() method is the entry point of a Java application, and the JVM starts execution from this method.

Detailed Answer

The main() method is declared as public static void main(String[] args) so that the JVM can access it without creating an object. It does not return any value and accepts command-line arguments for runtime input.

Key Takeaway

The main() method is mandatory for standalone Java applications. Its exact signature allows the JVM to locate and start program execution correctly.