JAVA • GETTING STARTED AND TOOLING

Main Method & Program Entry — Create and run a Java program with a main method (public static void main)

Every Java application begins with one special method — learn exactly why and how it works.

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.

1972
C Language Introduces main()
Dennis Ritchie creates C at Bell Labs. The main() function becomes the standard entry point for programs, a convention that influences decades of language design.
1991
Java Project Begins
James Gosling starts the "Green Project" at Sun Microsystems, aiming to create a platform-independent language. The team adopts C-style syntax, including the main method pattern.
1995
Java 1.0 Released
Java launches with the signature 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.
2023
JEP 463 — Simplified Main (Preview)
Java 21 introduces a preview feature allowing simpler program entry with unnamed classes, but the traditional main method remains the foundation for all production Java code.

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.

1

public

This access modifier means the method can be called from anywhere. The JVM needs to access main from outside your class, so it must be public.
2

static

The static keyword means the method belongs to the class itself, not to an object. Since no objects exist yet when the program starts, main must be static.
3

void

The return typevoid means this method does not return a value. When main finishes, the program simply ends — there is nothing to send back.
4

main

This is the exact method name the JVM searches for. It is case-sensitive — Main or MAIN will not work.
5

String[] args

The parameter is an array of Strings called args. It holds any command-line arguments passed to the program when it starts.
✦ KEY TAKEAWAY
Think of the main method like the front door of a house. A house can have many rooms (methods) and many hallways (classes), but when a guest (the JVM) arrives, they always enter through the front door. If the front door is missing, locked (not public), or labeled incorrectly, the guest cannot enter — and your program will not run.

Visual Explanation — Anatomy of the Main Method

Each keyword in the main method signature has a distinct role. The access modifier (public) allows the JVM to find it, static removes the need for an object, void means no return value, main is the exact name the JVM expects, and String[] args accepts input from the command line.

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.

This flowchart shows the complete lifecycle: you write a .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.
⚠️ Common Gotcha
The file name must match the class name exactly (including capitalization). If your class is called 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-by-line breakdown of HelloWorld.java
LineCodePurpose
1public 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.

Creating a Greeting Program
1
Step 1 — Create the Source FileOpen a text editor and create a file named 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!"); } } }
2
Step 2 — Compile the ProgramOpen a terminal or command prompt, navigate to the folder containing Greeter.java, and run the compiler. If there are no errors, a Greeter.class file is created silently — no output means success.
javac Greeter.java
3
Step 3 — Run Without ArgumentsRun the program without passing any arguments. Since args.length is 0, the program takes the else branch and prints the default greeting.
$ java Greeter → Output: Hello, World!
4
Step 4 — Run With an ArgumentNow pass a name as a command-line argument. The JVM stores "Alex" in args[0], so the program uses it to personalize the greeting.
$ java Greeter Alex → Output: Hello, Alex!
5
Step 5 — Understand the FlowThe JVM loaded 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.
Complete lifecycle: write → compile → run → output

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.

The five most common main method errors beginners encounter
MistakeWhat You WroteError Message / ResultFix
Missing staticpublic void main(String[] args)Main method is not staticAdd static before void
Wrong return typepublic static int main(String[] args)Main method must return voidChange int to void
Wrong parameter typepublic static void main(int[] args)Main method not foundUse String[] for the parameter type
Misspelled namepublic static void Main(String[] args)Main method not found (case-sensitive)Use lowercase main
File name mismatchClass: Hello, File: hello.javaCompiler error: class Hello is public, should be declared in Hello.javaRename the file to match the class name exactly
✦ KEY TAKEAWAY
Think of the main method signature like a password. It must be 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.

How the main method connects to advanced Java topics
ConceptWhat You Know NowWhat You'll Learn Later
MethodsThe 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 & ObjectsMain lives inside a class and is static (no object needed).Most Java code uses objects — main often creates them and calls their methods.
Multiple ClassesYour program has one class with one main method.Large programs have many classes, but only one main method serves as the entry point.
Packages & ImportsYou 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.
IDEsYou 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

PROBLEM 1 — CONCEPTUAL
Why must the main method be declared static? What would happen if you removed the static keyword?
PROBLEM 2 — BASIC CALCULATION
Look at the following code. Identify the error and write the corrected version. public class Adder { public void main(String args) { System.out.println(2 + 3); } }
PROBLEM 3 — INTERMEDIATE
Write a complete Java program in a class called 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?
PROBLEM 4 — APPLIED
Your friend sends you a file called 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); } }
PROBLEM 5 — CRITICAL THINKING
A Java project has three classes: 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.

Varsity Tutors • Java • Main Method & Program Entry