Java 21 Unnamed Classes and Instance Main Methods

Java 21 introduces two language core features: Unnamed Java Classes and a new launch protocol which allows running Java classes in a simpler format. In this article we will cover in detail these new features explaining how they can simplify your daily Java coding.

Java 21 introduces two core features that will simplify the coding of Java applications that will help both beginners and advanced Java scripters:

  • Unnamed Java Class. This feature allows to use your main entry point in an unnamed Class file. Therefore, you will not need to wrap the execution method inside a Class declaration
  • Enhanced launch protocol: You can now use a simple non-static main method declaration as launch method. This method does not require any input parameters, as entry point for your Class.

That being said, let’s see how to do that in practice.

Unnamed Classes and Instance Main Methods in practice

Firstly, download Java 21 from https://jdk.java.net/21/

If you prefer, you can skip this step and let JBang download Java 21 for you (check the next paragraph: “Java 21 with JBang”)

Next, edit a file “Hello.java“:

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

As you can see from the above sample code, we are using both new features available in Java 21. (Unnamed Class and simpler main method).

Then, let’s compile our Classe. Since this is a preview feature available in Java 21 we will add both the –release 21 and –enable-preview flag:

javac  --release 21 --enable-preview Hello.java

Finally, we can run it as follows:

java --enable-preview Hello

As you can see from the above example, when using an unnamed Java class, you can launch it using the File name which contains your Java code. In the above example, you can launch it as Hello Java Class.

After covering the basics, let’s see some examples of what you can do and what you cannot do in an unnamed Java Class.

Things you cannot do in an unnamed Java Class

One thing you cannot do is using a package declaration in your Java code:

package sample;

    void main() {
         System.out.println("Hello Java 21!");
    }

The above, will cause the following compilation error:

java 21 new features unnamed class step by step example

For that to work, you should wrap the main method in a Class as in this example:

package sample;

class HelloWorld { 
    void main() {
         System.out.println("Hello Java 21!");
    }
}

Another limitation is that you cannot use a Constructor with an unnamed Java class:

public Hello() {
      System.out.println("Constructor!");
}
void main() {
        new Hello();
}

The above will clearly trigger a compilation error:

java 21 unnamed class explained

Things you can do in an unnamed Class

After mentioning the limitations of unnamed Java Class files, let’s check what you can do, just like you would be in a regular Java Class.

Firstly, you can use the import statement to refer to external packages:

import java.util.ArrayList;

void main() {
        ArrayList list = new ArrayList();
        list.add("name"); 
        System.out.println(list.get(0));
}

Also, you can use variables and methods pretty much an ordinary Java Class (including the private declaration) :

private String message="done!";

public void doSomething() {
   System.out.println(message);
}
void main() {
   doSomething();
}

Finally, you can still use an array argument for your main entry method, just like an ordinary Java Class:

void main(String arg[]) {
    if (arg.length > 0) {
       System.out.println("First argument is "+arg[0]);
    }
    System.out.println("Hello world!");
}

Java 21 with JBang

If you are running JBang version v0.108.0 or newer then you can enable this preview feature also in JBang scripts. To learn how to install or upgrade JBang you can check this cheatsheet: JBang Cheatsheet (2023)

For example, edit the following main.java file:

//PREVIEW
//JAVA 21+

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

Notice the usage of the PREVIEW and JAVA 21+ directive comments which will let you use Java 21 preview features.

You can then run the script with just:

jbang main.java

Conclusion

Unnamed Classes and Instance Main Methods are an useful feature for programmers that are new to Java, or simply to speed up the creation of scripts written in Java. With the addition of JBang, writing scripts in Java becomes even more concise, without any programming-in-the-large scaffolding.

Found the article helpful? if so please follow us on Socials