Will Java have no more “public static void main()”?

Huypc
Goalist Blog
Published in
3 min readJul 29, 2023

When I write this blog, the newest stable version of Java is 20. The content below is part of Java 21 which is still candidate at the moment.

Why will Java have no more “public static void main()”?

This evolves the Java language so that students can write their first programs without needing to understand language feature designed for large problems.

Prerequisite

  • A text editor (Note, VS Code).
  • You need to install JDK 21 here.
  • Remember to install the suitable version for your OS (Mac/Win/Linux).

Let’s dirty your hands

First of all, you need to verify that you use the correct version of Java.

JDK 21

As you can see the above image, the version that I am using is 21-ea (Early Access).

At the current directory, I created a file named Main.java with the below code.

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

If your VS Code warns you, do not worry about it, that’s due to old version of Java you have setup in VS Code. In my case, there is no warnings.

Then, run:

$ java --source 21 --enable-preview Main.java

This is a preview language feature, disabled by default so that you will need some flags to compile and run it.

The result is:

Now, you can see that the application run successfully with the shortest Java code. It’s really good for beginner or someone completely new to Java.

However, to be careful of this feature. It may lead to misunderstand how a Java application work. In the beginning, whoever has learnt Java knows that the entry point of a Java application is a class having a public static main method with string parameters.

In comparison, the old version below:

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

There is too much clutter here — too much code, too many concepts, too many constructs — for a simple application does.

  • The class declaration and the mandatory public access modifier are programming-in-large constructs. They are useful when encapsulating a code unit with well-defined interface to external components. However, in this case, that is rather useless.
  • String[] args parameter also exists with no usage and it can make the novice confuse where this args is used.
  • The static modifier is a pain point too. It’s part of class-and-object model. In the beginning, that will be a mystery for students. In addition, to add more methods or fields that main can call and use, the student must either declare them all as static. It confronts the difference between static and instance members.

How does it select a main method?

When launching a class, the launch protocol chooses the first of the following method to invoke:

  1. A static void main(String[] args) method of non-private access (i.e., public, protected or package) declared in the launched class.
  2. A static void main() method of non-private access declared in the launched class.
  3. A void main(String[] args) instance method of non-private access declared in the launched class or inherited from a superclass.
  4. A void main() instance method of non-private access declared in the launched class or inherited from a superclass.

If the selected main is an instance method and is a member of an inner class, the program will fail to launch.

Under the hood, when we do not declare the class, Java compiler will assign the file name to the launched class which has the main method. That will not break any convention/manner of Java language.

References

JEP 445: Unnamed Classes and Instance Main Methods (Preview) (openjdk.org)

https://youtu.be/7Qlj62VSeb4

--

--