In **Java SE 22**, the `main()` method is the **entry point** of any standalone Java application. It's the first method that gets called when you run your program.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java SE 22!");
}
}
---
### ๐น Signature Breakdown
public static void main(String[] args)
Keyword |
Meaning |
public |
Accessible from anywhere. Required so the JVM can invoke it. |
static |
Belongs to the class, not an object. JVM doesn't instantiate your class to call it. |
void |
Returns nothing. |
main |
Special method name recognized by the JVM as the application entry point. |
String[] args |
Command-line arguments passed when you run the program. |
### ๐ธ JVM and `main()`
When you run:
java HelloWorld
the JVM looks for:
public static void main(String[] args)
If it's missing or incorrectly declared, youโll get:
Error: Main method not found in class HelloWorld
---
### ๐น Example With Arguments
public class CommandLineDemo {
public static void main(String[] args) {
if (args.length > 0) {
System.out.println("First argument: " + args[0]);
} else {
System.out.println("No arguments passed.");
}
}
}
Run it from command line:
java CommandLineDemo Hello
Output:
First argument: Hello
---
๐ธ Java SE 22 Enhancements?
While the core behavior of `main()` hasn't changed in Java SE 22, there is support for **simplified launchers** using single source-file programs:
java HelloWorld.java
No `javac` needed โ the Java 22 launcher compiles and runs `.java` files in one step.
Think of `main()` as the front door to your program. When Java starts your application, it knocks on this door and expects to be welcomed with exactly the right phrase โ `public static void main(String[] args)` โ or it wonโt come in.
The
main()
method is the
entry point for a Java program.
It is the first method that is invoked when a Java program is run.
A correctly defined
main()
method has the following
signature: [1]
main method on the exam
public static void main(String[] args) {
// The statements of the method are put here.
}
The
modifiers[2]public
and
static
, and the
void
return type must precede the
main()
method name. The
main()
method has a single argument that is an array of
String
objects. The
args
identifier is used by custom as the argument name.
However, any other valid identifier may be used in place of
args
.
Only Java application programs need a main() method. Applets are not required to have a main() method since they are executed by browsers within the context of a Web page.
The argument of the
main()
method provides access to the program's command line arguments. For example, suppose that a program is invoked with command line arguments of
abc
,
def
, and
ghi
. Then
args[0]
would reference the String
"abc"
,
args[1]
would reference
"def"
, and
args[2]
would reference
"ghi"
. The
MainTest program provides an example of using the
main()
method to access command line arguments.
The
MainTest
program shows how the
args
array is used to access command line arguments.
Compile it and run it with the following command line:
java MainTest zero one two three
It displays the following output:
args.length: 4
args[0]: zero
args[1]: one
args[2]: two
args[3]: three
class MainTest {
public static void main(String[] args) {
System.out.println("args.length: "+args.length);
for(int i=0;i<args.length;++i)
System.out.println("args["+i+"]: "+args[i]);
}
}
The first requirement in creating an executable Java application is to create a class with a method whose signature (name and method arguments) match the main method, defined as follows:
public class HelloExam {
public static void main(String args[]) {
System.out.println("Hello exam");
}
}
This main method should comply with the following rules:
- The method must be marked as a public method.
- The method must be marked as a static method.
- The name of the method must be main.
- The return type of this method must be void.
- The method must accept a method argument of a String array or a variable argument of type String.