Java 21— No more public static void main()

Breaking the mold with the new main() method.

Shwetha Narayan
3 min readAug 1, 2023

That’s right. In Java 21, the public static void main() method is no longer required.

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

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.

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.

--

--