Of course! Let's break down the Java main class, starting from the very basics and moving to more advanced concepts.

What is a main Class?
In Java, a main class is not a special type of class defined by a keyword. Instead, it's a standard Java class that has one specific requirement: it contains the main method.
This main method is the entry point of your Java application. When you run your program from the command line (or an IDE), the Java Virtual Machine (JVM) looks for this specific method to start executing your code.
The main Method Signature
The main method has a very strict and unchangeable signature. If you don't get it exactly right, your program won't run.
public static void main(String[] args)
Let's break down each part of this signature:
| Keyword/Part | Meaning | Why is it necessary? |
|---|---|---|
public |
Access Modifier | The JVM needs to be able to call this method, even if it's in a different package. public makes it accessible from anywhere. |
static |
Modifier | The JVM calls this method without creating an instance (an object) of the class first. A static method belongs to the class itself, not to any object. |
void |
Return Type | The main method doesn't return any value to the JVM when the program finishes. It just exits. |
main |
Method Name | This is the specific name the JVM is programmed to search for. It's case-sensitive (Main or MAIN will not work). |
(String[] args) |
Parameter | This is an array of String objects. It's how you can pass command-line arguments to your program when you run it. |
A Complete, Simple Example
Here is the simplest possible "Hello, World!" program, which demonstrates a main class.
File: MyFirstProgram.java
// 1. Declare a class. The file name MUST match the class name.
public class MyFirstProgram {
// 2. This is the main method - the entry point of the program.
public static void main(String[] args) {
// 3. This is the code that gets executed when the program runs.
System.out.println("Hello, World!");
}
}
How to Compile and Run It:
- Save: Save the code in a file named
MyFirstProgram.java. - Open a Terminal: Open a command prompt or terminal.
- Navigate: Go to the directory where you saved the file.
cd path/to/your/folder
- Compile: Use the Java compiler (
javac) to create the bytecode file (.class).javac MyFirstProgram.java
This will create a file named
MyFirstProgram.class. - Run: Use the Java Virtual Machine (
java) to execute the code.java MyFirstProgram
Expected Output:
Hello, World!
Important Note: Notice that when you run the program, you do not include the
.classextension. You are telling thejavacommand which class to run, not which file to execute.
Using Command-Line Arguments (args)
The String[] args parameter allows you to pass information to your program when you start it. Let's see an example.
File: Greeter.java
public class Greeter {
public static void main(String[] args) {
// Check if any arguments were provided
if (args.length > 0) {
// Loop through all the arguments and greet each person
for (int i = 0; i < args.length; i++) {
System.out.println("Hello, " + args[i] + "!");
}
} else {
// If no arguments are provided, give a default message
System.out.println("Hello, stranger!");
}
}
}
How to Compile and Run It with Arguments:
- Compile:
javac Greeter.java
- Run (with arguments):
java Greeter Alice Bob Charlie
Expected Output:
Hello, Alice!
Hello, Bob!
Hello, Charlie!
Key Concepts and Best Practices
a) One main per Project (Usually)
While you can have multiple classes with a main method in a single project, it's a common convention to have only one. When you run your program, you specify which class's main method you want to execute.
# To run the main method in class A java A # To run the main method in class B java B
b) The Class Name and File Name Must Match
The public class name in your code must exactly match the filename (including the capitalization).
// This code MUST be in a file named CorrectName.java
public class CorrectName {
public static void main(String[] args) {
System.out.println("This is correct.");
}
}
c) The main Method is for Applications
The main method is the entry point for a standalone Java application (like a desktop program or a command-line tool). It's not used in other contexts like:
- Java Applets: (Largely obsolete)
- Java Web Applications (Servlets): The entry point is a
web.xmlfile. - Java EE / Jakarta EE: Entry points are defined by the framework (e.g.,
@ApplicationPathfor JAX-RS).
d) Modern Java: var (Java 10+)
Starting with Java 10, you can use the var keyword for local variable type inference, which can make the code cleaner. You can use it inside the main method.
public class ModernMain {
public static void main(String[] args) {
var message = "Hello from a modern main!";
System.out.println(message);
// You can also use var for the args loop
for (var arg : args) {
System.out.println("Argument: " + arg);
}
}
}
Note: You cannot use var for the method parameters themselves (String[] args must stay as is).
Advanced: The main Method in a Non-Public Class
What if your class isn't public? It will still work as long as it's the only class in the .java file.
File: AnotherMain.java
// This class is NOT public. It's package-private (default access).
class AnotherMain {
public static void main(String[] args) {
System.out.println("A main method in a non-public class works!");
}
}
You can compile and run this exactly the same way:
javac AnotherMain.java java AnotherMain
This is useful for small utility classes or test files that you don't need to be accessible from other packages.
