Java 21, JEP 445, Unnamed Classes and Instance Main Methods
Tags: java, openjdk, jep
Published: 2023-09-20 06:18:13

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 post I want to highlight one of those preview features: [Java Enhancement Proposal (JEP) 445: "Unnamed Classes and Instance Main Methods"](https://openjdk.org/jeps/445). It’s a preview feature, meaning you need extra flags to use it. The goal of this 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. 

{% embed https://www.youtube.com/watch?v=Wrx7iVCVTwg %}

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

The video above and this post are inspired by [a post by Tom Cools](https://www.tomcools.be/post/april-2023-onboarding-now/), 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"](https://openjdk.org/jeps/330). 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:


![Running Java code with java thanks to JEP 330](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k7uweq7hlkmzg34bldkc.png)

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](https://www.jbang.dev/), 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](https://pi4j.com/examples/jbang/).

## 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:

```java
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:

```java
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:

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

### Foojay Podcast: "Java 21 Has Arrived!"

In [Foojay Podcast #28: "Java 21 Has Arrived!"](https://foojay.io/today/foojay-podcast-28), 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.

<iframe src='https://app.springcast.fm/player/episode/71857?theme=springcast&amp;chapter=15' width='100%' height='241' frameborder='0' scrolling='no' seamless='true' style='width:100%; height:241px;'></iframe>

## Tools to Install Java

The [OpenJDK project](https://github.com/openjdk/jdk) 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](https://sdkman.io/) and [JDKMon](https://github.com/HanSolo/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"](https://foojay.io/today/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!


![Meme... There is a newer version than 8?](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r3nn9uxlp5fpl27khv06.jpg)