Friends of OpenJDK Today

Java 21 – JEP 445 – Unnamed Classes and Instance Main Methods

October 25, 2023

Author(s)

  • Avatar photo
    Frank Delporte

    Frank Delporte (@frankdelporte) is a Java Champion, Java Developer, Technical Writer at Azul, Blogger, Author of "Getting started with Java on Raspberry Pi", and Pi4J Contributor. Frank blogs about his ... Learn more

Java 21, released on September 19th, 2023, brings many new features, 8 which are fully integrated and 7 which are incubator or preview.

In this article, I want to highlight one of those preview features: Java Enhancement Proposal (JEP) 445: "Unnamed Classes and Instance Main Methods".

It’s a preview feature, meaning you need extra flags to use it. The goal of JEP 445 is to make it easier to get started with Java, as it’s all about reducing the number of keywords when you write, for instance, your very first HelloWorld Java code.

It’s ideal for students or anyone who wants to start experimenting with Java. It may also help make Java more popular in boot camps, where JavaScript and Python dominate now.

JEP 330: "Launch Single-File Source-Code Programs"

The video above and this post are inspired by a post by Tom Cools, and to be honest, I stole a bit of his approach as I first want to show you the older JEP 330: "Launch Single-File Source-Code Programs".

Thanks to this feature, introduced in Java 11 you can already execute a single Java-file without the need to compile the code to byte code with javac as visiualized in this diagram:

With the changes brought by JEP 330, the conversion from Java code to byte code, is "hidden". It still happens, but you, as a user, don't need to take care of it.

JBang

Thanks to this JEP 330, it already became a lot easier to run Java code with one command, but it also has a limitation. When that single file application needs dependencies, you still need to go for a complete Maven or Gradle project to define these dependencies and versions.

Not related to the JEP, but a possible alternative solution in this case is JBang, it’s a tool also directed towards students and others experimenting with Java. It has a syntax to define that it needs to be handled as a script and can define dependencies to be included. I used JBang to create multiple tutorials in the Pi4J project.

JEP 445: "Unnamed Classes and Instance Main Methods"

Now it's finally time to talk about the "main" subject of this post, JEP 445.

Code Examples

Until now, a minimal "Hello World" Java application, needs this code:

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

Although only one line produces the output, there is a lot of "clutter" around it that needs to be explained, or in most cases is taken "as is" and doesn't get explained to newbies. Thanks to JEP 445, that same code can now be simplified to:

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

Because this is a preview feature, additional flags are needed to execute this code. Thanks to JEP 330, we don't need to compile it. If you save the above code in a file HelloWorld.java, it can be executed directly, with a Java version 21, with java like this:

java --source 21 --enable-preview HelloWorld.java

Foojay Podcast: "Java 21 Has Arrived!"

In Foojay Podcast #28: "Java 21 Has Arrived!", I talked with Simon Ritter, Mohamed Taman, and Piotr Przybyl about the many new features in Java 21. Not all of them find this JEP 445 the most interesting one, but they all agree it makes it easier to introduce the language to new people.

Tools to Install Java

The OpenJDK project only contains the sources for the full Java-project. The actual runtime and SDK are available from many different distributors.

Make sure to check out SDKMan and JDKMon to easily install Java on your system, switch between versions, and monitor installed versions.

More info about this topic is available on this Foojay post: "Disco API: Helping You To Find Any OpenJDK Distribution".

Conclusion

This JEP 445 won't bring many improvements for experienced developers, but it may bring Java closer to education and help to experiment with the language.

As it is a preview feature, you still need additional flags to use it, but hopefully the next Java-release will bring this to its full use!

As always, start using the new Java version ASAP for your development and try to update your production systems so you can take full advantage of all the evolutions in the OpenJDK project!

Related Articles

View All

Author(s)

  • Avatar photo
    Frank Delporte

    Frank Delporte (@frankdelporte) is a Java Champion, Java Developer, Technical Writer at Azul, Blogger, Author of "Getting started with Java on Raspberry Pi", and Pi4J Contributor. Frank blogs about his ... Learn more

Comments (0)

Your email address will not be published. Required fields are marked *

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Save my name, email, and website in this browser for the next time I comment.

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard