For many decades, Java programmers have relied on the public static void main()
method as the entry point for Java applications. However, Java 21 introduces a new feature that simplifies the coding process.
As you know, in Java, the main()
method serves as the starting point for all programs. When a Java program is executed, the Java Virtual Machine (JVM) invokes the main()
method to initiate the application.
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Java 21 now offers a more streamlined approach to defining the entry point of your application, eliminating the need for public static void main()
. This enhancement reduces boilerplate code and improves readability, making Java more accessible.
Syntax and Usage
In Java 21, you can write a Java program without explicitly defining the main
method. Instead, you can place your executable code directly within the class body. Here’s an example:
public class HelloWorld {
static {
System.out.println("Hello World!");
}
}
Prior to Java 21, although static blocks existed, the main method was still required to start the application. With Java 21, this is no longer necessary.
1