01 / 21
{ main ▶ run

Main Method &
Program Entry

Learn to create and run a Java program with the public static void main method: the gateway every Java application uses to start executing code.

Java

What Is a Program Entry Point?

Every application needs a starting point: the first instruction the computer executes when you click "Run." In Java, that starting point is always a special method called main.

How the JVM Finds Your Code

You write HelloWorld.java javac Compiler JVM Looks for main() ▶ Source code Compiles to bytecode Executes main()

The Java Virtual Machine (JVM) is the engine that runs your code. When you execute a program, the JVM searches for a method with the exact signature public static void main(String[] args) and begins execution there.

Why Every Java Program Needs main

Contract
Java is a compiled language. The JVM expects a specific contract: one method it can always call to start execution.
Consistency
Unlike scripting languages where any line can run first, Java guarantees a single, predictable entry point.
Scalability
Large projects have thousands of classes. The main method tells the JVM which class starts the application.

The Exact Signature

Java
public static void main(String[] args)
public static void main String[] args Accessible to the JVM No object needed Returns nothing Method name Command-line arguments

Breaking Down Each Keyword

1
public: The method must be visible from outside the class so the JVM can call it without restriction.
2
static: The JVM calls main without creating an object. Static methods belong to the class, not an instance.
3
void: The method does not return a value. When main finishes, the program simply ends.
4
String[] args: An array of Strings that holds any command-line arguments passed when running the program.

static vs Instance Methods

static Method Instance Method
Belongs to The class itself An object (instance)
Called via ClassName.method() object.method()
Object needed? No Yes
Why main uses it JVM must call main before any objects exist

Writing Your First Program

1
Create a file named exactly the same as your class (e.g., HelloWorld.java).
2
Declare a public class with the same name as the file: public class HelloWorld.
3
Add the main method inside the class with the exact signature: public static void main(String[] args).
4
Write your code inside main's curly braces { }.
5
Compile with javac HelloWorld.java, then run with java HelloWorld.

Example 1: Hello World

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

Example 2: Using Command-Line Args

Greeter.java
public class Greeter {
    public static void main(String[] args) {
        if (args.length > 0) {
            System.out.println("Hello, " + args[0] + "!");
        } else {
            System.out.println("Hello, stranger!");
        }
    }
}

Example 3: Calling Other Methods

Calculator.java
public class Calculator {

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

    public static void main(String[] args) {
        int result = add(5, 3);
        System.out.println("Sum: " + result);
    }
}

Anatomy of a Java Program

MyProgram.java public class MyProgram { public static void main(String[] args) { // Your code starts here System.out.println("Running!"); ◀ JVM enters here } } Class declaration main method

What Happens Without main?

Broken.java
public class Broken {
    public void greet() {
        System.out.println("Hi!");
    }
}

This compiles successfully with javac, but when you try to run it with java, the JVM throws an error because it cannot find the entry point.

Understanding String[] args

$ java MyApp Alice 25 true args = "Alice" [0] "25" [1] "true" [2] All values are Strings, even numbers and booleans

Common Mistakes

✕ Forgetting static
Writing public void main(...) compiles but the JVM cannot call it without an object.
✕ Misspelling main
Names like Main, mian, or maim will not be recognized as an entry point.
✕ Wrong parameter
Using int[] args or String args instead of String[] args breaks the signature.
✕ Returning a value
Changing void to int or String means it no longer matches the required signature.

Signature Cheat Sheet

Keyword Meaning What Happens If Changed
public JVM can access it JVM can't find method
static No object required Runtime error: no main
void Returns nothing Signature mismatch
main Entry point name JVM doesn't find entry
String[] args CLI arguments Wrong parameter type

Question 1 of 5

Which of the following is the correct signature for a Java program's entry point?

Question 2 of 5

Why must the main method be declared static?

Question 3 of 5

What happens when you run java Demo?

Question 4 of 5

If you run java MyApp cat dog, what is the value of args[1]?

Question 5 of 5

What happens when you compile and run this program?