Java 21— No more public static void main()

Breaking the mold with the new main() method.

Shwetha Narayan
6 min readAug 1, 2023

That’s right. Starting from Java 21, you can run classes without the traditional public static void main(String[] args) method in certain contexts. This enhancement allows for more flexibility, particularly for scripting and small applications.

This new feature allows developers to write simple programs without requiring the boilerplate code public static void main(String[] args). This enhancement is known as Plain Main Programs and is part of Project Leyden. It makes Java more beginner-friendly and less verbose for small applications.

This is a new feature that was introduced in Java 21 to simplify the coding of Java applications.

You can directly execute Java source files that don’t have a main method using the command line, which makes it easier to create quick scripts and simplifies the development process for beginners.

However, the traditional main method remains fully supported and is still required for larger, structured applications. This means you can choose the approach that best suits your needs, whether you prefer the classic structure or the newer, more flexible options.

You see, in Java, the main() method is the starting point for all Java programs. When you run a Java program, the Java Virtual Machine (JVM) calls the main() method to kick things off.

Harnessing Static Blocks: A Longstanding Approach to Running Java Code

You’re right! The ability to run Java code using static blocks or other non-main constructs has been around for a while. However, Java 21 formalizes the ability to run classes without a main method more straightforwardly, especially for script-like executions.

In earlier versions, you could use static blocks or other mechanisms to execute code, but the process was less intuitive. Java 21 enhances this by allowing direct execution of Java files without requiring the main method explicitly, making it easier for quick scripts and educational purposes.

In earlier versions of Java, you could not directly execute Java files without a main method explicitly defined. The public static void main(String[] args) method was required as the entry point for any standalone Java application.

How it works:

When writing a Java program, you can now omit the public static void main method altogether. The JVM will automatically recognize the entry point of your program based on the code structure, making it more streamlined.

Example:

Instead of writing this:

public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

You can write:

System.out.println("Hello, World!");

This is particularly useful for small scripts or educational purposes, as it reduces the amount of boilerplate code needed to get started with Java.

Now, why would you want to ditch the main() method?

Well, there are a couple of reasons.

Simplicity : First , it makes it simpler for beginners to learn Java. The main() method is often seen as a barrier to entry for new programmers, because it can be confusing to understand.

The main() method is a specific method that is required for all Java programs. It has a specific signature, and it must be declared in a certain way. This can be confusing for beginners, who may not understand the purpose of the main() method or how to declare it correctly.

For example, the main() method must be public, static, and void. It must also take an array of strings as a parameter. This can be a lot to remember for beginners, and it can make it difficult to write a simple Java program.

To be more consistent with other Programming Languages

Second, this change makes Java more consistent with other programming languages. In lots of other languages, the program’s entry point isn’t a specific method like main(), but rather a block of code executed at the beginning.

And get this — Java’s trying to be all hip and consistent with other programming languages like Python and JavaScript. Those languages don’t bother with a main() method, why should Java?

Flexibility: Lastly, it opens up more possibilities for how you structure your Java programs. You can now create Java programs without a main class, giving you more flexibility.

You can now create programs without a main class, and they can also use unnamed classes. This can make programs more modular and easier to understand.

Before, you had to follow this strict way of declaring the main() method, and it tied your hands when you wanted to try out something new. But now, without the main() method, you’re free to explore different programming styles.

For example, some programming styles, such as functional programming, do not use the main() method at all. In functional programming, programs are built up from small, reusable functions, and there is no need for a central main() method to start the program.

Here is a sample code that you can use to run a Java program without the public static void main() method:

class HelloWorld {

void main() {
System.out.println("Hello, world!");
}

}

Now , let’s make it even better:

Unnamed classes , a new feature in Java 21 allows you to write simple Java programs without having to declare a class.

For example, a developer could create a program that consists of a number of small, self-contained functions. Each function could be defined in its own unnamed class, and the functions could be called from the main method. This would make the program more modular and easier to understand, as each function would be responsible for a specific task.

To create an unnamed class, you simply need to write a method that starts with the keyword new. For example, the following code would create an unnamed class that prints "Hello, world!" to the console:

new {
System.out.println("Hello, world!");
}

Overall, the removal of the main() method is a positive change for Java. It makes the language more accessible, modern, and flexible. If you are a Java developer, I encourage you to give it a try.

Things to know before running the code:

1. Preview Features:

Java Preview features : Java has a history of introducing new language features through a preview mechanism. This allows developers to experiment with and provide feedback on potential changes before they become permanent . (You can read more about “Preview features” here : )

Preview features in Java are not enabled by default. To use them, you need to explicitly activate them using specific flags (through command-line or IDEs) during compilation and runtime. This ensures that developers are aware of the experimental nature of these features and can make informed decisions about their usage.

Here’s how you can run it?

To run a program without the public static void main(String[] args) method in Java 21, there are a couple of things to check:

1. Ensure you’re using Java 21 or higher

This feature is available starting with Java 21. First, ensure that your system is indeed running Java 21 by checking your version:

java --version

If it doesn’t show Java 21, you’ll need to update your JDK.

2. Check for preview feature

This simplified syntax is a preview feature in Java 21. Preview features are not enabled by default, so you must tell the compiler and runtime to allow them.

Steps to enable preview features:

  • Compile with preview features enabled: You need to compile your program with the --enable-preview flag. Here's how you do it using javac:
javac --enable-preview --release 21 YourProgram.java
  • Run with preview features enabled: When you run your program, also include the --enable-preview flag:
java --enable-preview YourProgram

3. Check your IDE configuration

If you’re using an Integrated Development Environment (IDE) like IntelliJ IDEA, Eclipse, or Visual Studio Code, ensure that the IDE is configured to:

  • Use Java 21 as the project JDK.
  • Enable preview features in the build and run configurations.

A. To Configure IDE Run Configuration to enable preview :

i) Here’s an example for Eclipse IDE:
1. Go to Run As -> Run Configurations.
2. Go to Arguments section.
3. Under the VM options field, add “ — enable-preview” (without quotes).
4. Click Apply and Run.

ii) For IntelliJ IDEA, you can do the following:

  1. Go to Project Structure and ensure Java 21 is selected as the SDK.
  2. In Run Configurations, under Modify options, enable the --enable-preview option.

B. If this alone doesn't work, compile the code using the below in CMD prompt:

- release 21 flag javac - release 21 - enable-preview YourClassName.java

Once you enable preview features and configure your environment, your simple code without the public static void main method should work.

--

--