Historical Context & Motivation
Before Java existed, programmers already needed a way to tell the computer where a program should begin executing. In the 1970s, the C programming language established the convention of a main() function as the entry point — the very first piece of code the operating system would run. This idea was so effective that nearly every language since has adopted some version of it.
When James Gosling and his team at Sun Microsystems designed Java in the early 1990s, they kept C's familiar main method concept but wrapped it in Java's object-oriented structure. Every piece of Java code must live inside a class, so the main method became a special static method within a class. Understanding this entry point is the first step to writing and running any Java program.
main() function becomes the standard entry point for programs, a convention that influences decades of language design.public static void main(String[] args) as the required entry point. The JVM (Java Virtual Machine) looks for this exact method to start any application.The core question this lesson addresses is straightforward but essential: when you click "Run" on a Java program, how does the computer know where to start? The answer lies in the precise signature of the main method and the role of the Java Virtual Machine in locating and invoking it.
Core Principles & Definitions
Every Java program needs a clearly defined starting point. The main method is that starting point, and every keyword in its signature — public static void main(String[] args) — serves a specific purpose. To truly understand the main method, you need to know what each word means and why it is required.
public
static
void
void means this method does not return a value. When main finishes, the program simply ends — there is nothing to send back.main
Main or MAIN will not work.String[] args
args. It holds any command-line arguments passed to the program when it starts.Visual Explanation — Anatomy of the Main Method
As the diagram above shows, the main method is not just a random collection of words. Every keyword is required and must appear in a specific order. If you change public to private, the JVM cannot access it. If you remove static, the JVM would need to create an object first — but it does not know how to do that without running your code first. Each keyword solves a specific problem that makes program startup possible.
How It Works — From Source Code to Execution
When you write a Java program, your code goes through a two-stage journey before it actually runs. Understanding this process explains why the main method must have its exact signature and how the Java Virtual Machine (JVM) uses it to launch your application.
Stage 1: Compilation
You write your code in a file with a .java extension. The Java compiler (javac) reads this file and translates it into bytecode, which is stored in a .class file. Bytecode is a set of instructions that the JVM understands — think of it as a universal language that works on any computer with a JVM installed.
Stage 2: Execution
When you run java MyProgram in the terminal, the JVM loads your MyProgram.class file and searches for a method with the exact signature public static void main(String[] args). If it finds a match, execution begins on the first line inside that method. If it does not find a match, the JVM throws an error: Main method not found.
.java file, compile it with javac into .class bytecode, and then the JVM searches for the main method. If found, the program runs. If not, you get an error.HelloWorld, the file must be named HelloWorld.java. When running, you type java HelloWorld — without the .class extension.Detailed Breakdown — The Complete Hello World Program
Let's look at the simplest complete Java program — the classic Hello World — and break down every line. This is the program nearly every Java developer writes first, and it demonstrates every concept we have discussed so far.
| Line | Code | Purpose |
|---|---|---|
| 1 | public class HelloWorld { | Declares a public class named HelloWorld. All Java code lives inside a class. |
| 2 | public static void main(String[] args) { | Declares the main method — the entry point the JVM looks for. |
| 3 | System.out.println("Hello, World!"); | Prints the text "Hello, World!" to the console and moves to a new line. |
| 4 | } | Closes the main method. |
| 5 | } | Closes the HelloWorld class. |
Notice the nesting structure: the main method lives inside the class, and the println statement lives inside the main method. Java uses curly braces { } to define these boundaries, and proper indentation makes the structure easy to read.
What About String[] args?
The String[] args parameter lets you pass information to your program when you launch it from the terminal. For example, running java HelloWorld Alice Bob would set args[0] to "Alice" and args[1] to "Bob". Even if you never use command-line arguments, the parameter must still be declared — the JVM demands it as part of the signature.
Worked Example — Writing, Compiling, and Running Your First Program
Let's walk through the complete process of creating a Java program that takes a name from command-line arguments and prints a personalized greeting. This example demonstrates every step from writing code to seeing output.
Greeter.java. The file name must match the public class name. Type the following code:public class Greeter {
public static void main(String[] args) {
if (args.length > 0) {
System.out.println("Hello, " + args[0] + "!");
} else {
System.out.println("Hello, World!");
}
}
}Greeter.java, and run the compiler. If there are no errors, a Greeter.class file is created silently — no output means success.javac Greeter.javaargs.length is 0, the program takes the else branch and prints the default greeting.$ java Greeter → Output: Hello, World!"Alex" in args[0], so the program uses it to personalize the greeting.$ java Greeter Alex → Output: Hello, Alex!Greeter.class, found public static void main(String[] args), placed ["Alex"] into the args array, and executed the method from top to bottom. The program printed a line and then ended because there was nothing else to do.Common Errors & How to Fix Them
When you are starting out with Java, small mistakes in the main method signature can prevent your program from running. The JVM is very strict about the exact format. Here are the most common errors, what message you will see, and how to fix each one.
| Mistake | What You Wrote | Error Message / Result | Fix |
|---|---|---|---|
| Missing static | public void main(String[] args) | Main method is not static | Add static before void |
| Wrong return type | public static int main(String[] args) | Main method must return void | Change int to void |
| Wrong parameter type | public static void main(int[] args) | Main method not found | Use String[] for the parameter type |
| Misspelled name | public static void Main(String[] args) | Main method not found (case-sensitive) | Use lowercase main |
| File name mismatch | Class: Hello, File: hello.java | Compiler error: class Hello is public, should be declared in Hello.java | Rename the file to match the class name exactly |
public static void main(String[] args) — every word in the right order, with the right spelling and capitalization. Even one wrong letter and the JVM will not recognize it. Memorize it once and you will never be locked out of your own program.Connection to Advanced Topics
The main method is your starting point, but as you grow as a Java programmer, you will encounter related concepts that build on this foundation. Understanding where the main method fits in the bigger picture helps you see why Java is designed the way it is.
| Concept | What You Know Now | What You'll Learn Later |
|---|---|---|
| Methods | The main method is the first method that runs. | You can create your own methods and call them from main, organizing code into reusable pieces. |
| Classes & Objects | Main lives inside a class and is static (no object needed). | Most Java code uses objects — main often creates them and calls their methods. |
| Multiple Classes | Your program has one class with one main method. | Large programs have many classes, but only one main method serves as the entry point. |
| Packages & Imports | You use System.out.println to print output. | System is part of the java.lang package. You'll learn to import other packages for more features. |
| IDEs | You compile and run from the command line. | IDEs like IntelliJ and VS Code automate compilation and running, but they still use main under the hood. |
As of Java 21, the language introduced a preview feature for unnamed classes and instance main methods, which lets beginners write simpler programs without the full public static void main boilerplate. However, the traditional main method remains the standard for production code, and understanding it is essential for any serious Java development.
Practice Problems
static? What would happen if you removed the static keyword?public class Adder {
public void main(String args) {
System.out.println(2 + 3);
}
}Echo that prints all command-line arguments, each on a separate line. If no arguments are provided, it should print "No arguments given." What file name must you use?calculator.java containing the following code. They say it won't compile. Find and fix all problems.
public class Calculator {
public static void Main(String[] args) {
int result = 10 * 5;
System.out.println("Result: " + result);
}
}App, Helper, and DataLoader. Each class has a public static void main(String[] args) method. Is this legal Java? If so, how does the JVM know which main method to run? What are reasons you might put a main method in a class that is not the "primary" entry point?Lesson Summary
Every Java application begins with the main method, which must be declared as public static void main(String[] args). The public keyword makes the method accessible to the JVM, static allows it to be called without creating an object, void indicates no return value, and String[] args accepts command-line arguments. The JVM searches for this exact signature when you run a program — if any part is wrong or missing, your program will not start.
To create and run a Java program, you write your code in a .java file (whose name matches the class name), compile it with javac to produce bytecode in a .class file, and then execute it with the java command. Mastering this entry point is the essential first step to building any Java application.