The Main Method

Introduction

The main() method is the entry point of a Java program. Execution starts from main().


Syntax

public static void main(String[] args) {
    // Program code
}

Components Explained

1. public:

  • Access modifier
  • JVM can access from anywhere
  • Must be public for JVM to call it

2. static:

  • Called without creating object
  • JVM calls before any object creation
  • Belongs to class, not instance

3. void:

  • Returns nothing
  • main() doesn’t return value to JVM

4. main:

  • Method name
  • Must be exactly “main”
  • JVM looks for this specific name

5. String[] args:

  • Array of command-line arguments
  • Can be named anything (args, arguments, etc.)
  • Type must be String array

Basic Example

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

Valid main() Variations

// Standard
public static void main(String[] args) { }

// Array notation variations
public static void main(String args[]) { }
public static void main(String... args) { }  // Varargs

// Different parameter names
public static void main(String[] arguments) { }
public static void main(String[] a) { }

// Order: public and static can be swapped
static public void main(String[] args) { }

// With final
public static void main(final String[] args) { }

Invalid main() Methods

// Missing public
static void main(String[] args) { }  // ❌ Won't run

// Missing static
public void main(String[] args) { }  // ❌ Won't run

// Wrong return type
public static int main(String[] args) { }  // ❌ Won't run

// Wrong parameter type
public static void main(int[] args) { }  // ❌ Won't run

// Missing parameter
public static void main() { }  // ❌ Won't run

Command-Line Arguments

public class ArgsDemo {
    public static void main(String[] args) {
        System.out.println("Number of arguments: " + args.length);

        for (int i = 0; i < args.length; i++) {
            System.out.println("Argument " + i + ": " + args[i]);
        }
    }
}

Run:

java ArgsDemo Hello World 123

Output:

Number of arguments: 3
Argument 0: Hello
Argument 1: World
Argument 2: 123

Multiple main() Methods

Each class can have its own main() method.

// File: ClassA.java
class ClassA {
    public static void main(String[] args) {
        System.out.println("Main in ClassA");
    }
}

// File: ClassB.java
class ClassB {
    public static void main(String[] args) {
        System.out.println("Main in ClassB");
    }
}

// Run specific class
// java ClassA  -> "Main in ClassA"
// java ClassB  -> "Main in ClassB"

main() Method Flow

public class Demo {
    public static void main(String[] args) {
        System.out.println("1. Program starts");
        method1();
        System.out.println("4. Program ends");
    }

    static void method1() {
        System.out.println("2. In method1");
        method2();
    }

    static void method2() {
        System.out.println("3. In method2");
    }
}

Output:

1. Program starts
2. In method1
3. In method2
4. Program ends

Calling Instance Methods from main()

public class Calculator {
    // Instance method
    int add(int a, int b) {
        return a + b;
    }

    public static void main(String[] args) {
        // Must create object to call instance method
        Calculator calc = new Calculator();
        int result = calc.add(10, 20);
        System.out.println("Sum: " + result);
    }
}

main() in Different Contexts

1. Simple Program:

public class Simple {
    public static void main(String[] args) {
        System.out.println("Simple program");
    }
}

2. With Other Methods:

public class Program {
    public static void main(String[] args) {
        displayWelcome();
        int result = calculate(10, 20);
        System.out.println("Result: " + result);
    }

    static void displayWelcome() {
        System.out.println("Welcome!");
    }

    static int calculate(int a, int b) {
        return a + b;
    }
}

3. With Object Creation:

class Student {
    String name;
    int rollNo;

    void display() {
        System.out.println(name + " - " + rollNo);
    }
}

public class Test {
    public static void main(String[] args) {
        Student s = new Student();
        s.name = "John";
        s.rollNo = 101;
        s.display();
    }
}

Why main() is Static?

Reason: JVM needs to call main() before creating any object.

public class Demo {
    // If main was not static:
    // public void main(String[] args) {  // Wrong!
    //     // JVM would need to create object first
    //     // Demo obj = new Demo();
    //     // obj.main(args);
    //     // But how? No constructor called yet!
    // }

    // Static allows direct call:
    public static void main(String[] args) {
        // Demo.main(args) - No object needed
        System.out.println("Works!");
    }
}

Overloading main()

You can overload main(), but JVM only calls the standard signature.

public class OverloadMain {
    // Standard main - JVM calls this
    public static void main(String[] args) {
        System.out.println("Standard main");
        main(5);  // Call overloaded version
    }

    // Overloaded main - must be called explicitly
    public static void main(int x) {
        System.out.println("Overloaded main: " + x);
    }
}

main() Signature Requirements

ComponentRequiredNotes
publicYesJVM must access it
staticYesNo object needed
voidYesNo return value
mainYesExact name
String[]YesCommand-line args

Quick Reference

// Standard format
public class MyProgram {
    public static void main(String[] args) {
        // Entry point
        // Program execution starts here
    }
}

// Run:
// javac MyProgram.java
// java MyProgram

Common Errors

Error 1: Missing main()

public class NoMain {
    // No main method
}
// Error: Main method not found in class NoMain

Error 2: Wrong signature

public class WrongMain {
    public void main(String[] args) {  // Missing static
    }
}
// Error: Main method is not static

Error 3: Wrong parameter

public class WrongParam {
    public static void main(int[] args) {  // Wrong type
    }
}
// Error: Main method not found

Exam Tips

Remember:

  1. Signature: public static void main(String[] args)
  2. Entry point of program
  3. public: JVM can access
  4. static: Called without object
  5. void: No return value
  6. String[] args: Command-line arguments
  7. Must be exact signature
  8. Can be overloaded but JVM calls standard one
  9. Each class can have own main()
  10. First method JVM executes

Common Questions:

  • What is main() method?
  • Why is main() static?
  • Why is main() public?
  • Can we change main() signature?
  • What are command-line arguments?
  • Can we overload main()?
  • What if no main() method?