Core Java

Java Unnamed Class and Instance Main Method

In this blog post, we will learn about Java Unnamed Class and Instance Main Method in Java 21.

1. Introduction

Java has been cited to be a very verbose language for even a simple hello world program and small proof-of-concept works. Since Java JDK 21, we can use an unnamed class and instance main method that allows us to bootstrap a class with minimal syntax. This will be of benefit to beginners who have just started to learn Java and want to try out the language syntax for quick learning.

1.1 JDK 21 Changes at a Glance

JDK 21 is set to be generally available on 2023/09/19. This Reference Implementation of version 21 of the Java SE Platform is set to have the following 15 features

  • [JEP 430] String Templates
  • [JEP 431] Sequenced Collections
  • [JEP 439] Generational ZGC
  • [JEP 440] Record Patterns
  • [JEP 441] Pattern Matching for switch
  • [JEP 442] Foreign Function and Memory API
  • [JEP 443] Unnamed Patterns and Variables
  • [JEP 444] Virtual Threads
  • [JEP 445] Unnamed Classes and Instance Main Methods
  • [JEP 446] Scoped Values
  • [JEP 448] Vector API
  • [JEP 449] Deprecate the Windows 32-bit x86 Port for removal
  • [JEP 451] Prepare to Disallow the Dynamic Loading of Agents
  • [JEP 452] Key Encapsulation Mechanism API
  • [JEP 453] Structured Concurrency

This article concentrates on JEP(JDK Enhancement Proposal) 445 – Unnamed Classes and Instance Main Methods

2. Purpose and Benefits of Instance Main Methods and Unnamed Classes

Easing the learning experience for new users is one of the primary purposes of unnamed classes and Java instance methods. Instance main methods and unnamed classes provide a simple method for developers new to Java programming to learn Java in a manner that introduces concepts in the right order and quickly launches a Java application in Java 21. Listed below are some of the technical benefits of using instance main methods and unnamed classes:

  • No need to declare a class or declare an String[] array.
  • No arguments must be passed to the main method.
  • No need to use an access modifier public and use the static keyword.

3. Download and Setup Java JDK 21

For early access to Java JDK 21, visit the website at https://jdk.java.net/21/ and download the most recent build of the OpenJDK. Next, extract the contents of the downloaded zipped file and copy the contents to a suitable location on your local file system where you want the JDK 21 to reside.

3.1 Setting the Executable Path

After we are done downloading, extracting, and copying the contents of JDK 21 to a location directory, we need to carry out one more additional step of setting the executable path. To do this in UNIX (Including Mac OS X and Linux), open a terminal and add the following line:

export JAVA_HOME=/Path/To/JDKDirectory/jdk-21.jdk/Contents/Home

3.1.1 Verify Java JDK 21 Installation

To verify the JDK 21 installation on your system, open a terminal and type either of the following lines:

java -version

javac -version

On the terminal, we should get a display output that confirms a successful installation of JDK 21 as shown below

Fig 1: Verify Java JDK 21 Installation
Fig 1: Verify Java JDK 21 Installation

4. Instance Main Method and Unnamed Classes

4.1 Instance Main Methods

From Java 21, The way a simple Java application is launched using instance main method is being enhanced and simplified. Firstly, developers can now use a non-static main method declaration. A main() method does not require any String[] parameters, and does not require the public access modifier and the static keyword.

4.1.1 Flexible Ways to Write Main Methods

Prior to Java 21, writing a simple “Hello World” standalone application in Java looked like this:

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

From Java 21, when writing a simple Hello World program, developers can create and edit an HelloWorld.java file with the following code:

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

Note that we have written the above class without a class declaration. The unnamed class and instance main method feature removes the class declaration, public  access modifier and the static keyword giving the developer a simpler, more concise, and cleaner class.

Other ways to write a main method in Java 21 are:

protected static void main() {
  System.out.println("This is a valid method");
}

public void main(String[] args) {
  System.out.println("This is a valid method");
}
     
static void main(String[] args){
  System.out.println("This is a valid method");
}

static void main(){
  System.out.println("This is a valid method");
}

void main(String[] args){
  System.out.println("This is a valid method");
}

void main(){
  System.out.println("This is a valid method");
}

Note from the above code that programmers can still use an String[] arg array argument on their main() method.

4.2 Unnamed Classes

An unnamed class is a type of class that does not have a name and cannot be called by another class. Unnamed classes can contain methods and fields. The unnamed class feature allows a programmer to have an main() entry point in an unnamed Java Class file. A programmer will not need a Class declaration when writing a main() method as the entry point of a Java application

4.2.1 Creating an Unnamed Class

To create, compile and run an unnamed class in Java, first navigate to the folder where you want to save the file and create a file named HelloWorld.java. Next, edit HelloWorld.java file to look like this and save it.

String greeting = "Hello, World!";

void main() {
    System.out.println(greeting);
}

JEP 445 is a preview language feature and is disabled by default in JDK 21. To compile our code using JDK 21, we must enable preview features by adding –release 21 and –enable-preview flag. To compile the above code in Java 21, We can use either of the following commands to run the program

  • Compile the program with javac --release 21 --enable-preview HelloWorld.java and run it with java --enable-preview HelloWorld
  • Run the program with java --source 21 --enable-preview HelloWorld.java

We can launch the application using the file name which contains your Java code. In the above example, we can run the application as HelloWorld.java as shown below.

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

The output from running the above command on a terminal is:

Fig 2: running Java 21 on a terminal
Fig 2: running Java 21 on a terminal

4.3 Do’s and Don’ts When Using Unnamed Classes and Instance Main Methods

Things a programmer can do when using an unnamed class are:

  • The main entry point of a Java program in Java 21 can still use an array argument like this – void main(String arg[]).
  • The import statement can be used to refer to external packages just as you would prior to Java 21.
  • Variables and methods can be declared as usual just as you would on an ordinary Java Class prior to using Java 21 including the private keyword.

Things a programmer can’t do when using unnamed classes include:

  • You cannot use a package declaration like package com.javacodegeeks in your Java class. Declaring a package would lead to a compilation error.
  • You cannot use a Constructor with an unnamed Java class. This would lead to a compilation error.
  • The private visibility modifier cannot be used with the main() method. For example, private static void main() is not allowed.

5. Conclusion

In Conclusion, Using Java 21 with the unnamed classes and instance main methods feature, Java is easier to learn and new users can learn to write Java applications without needing to understand advanced language features designed for large programs.

Omozegie Aziegbe

Omos holds a Master degree in Information Engineering with Network Management from the Robert Gordon University, Aberdeen. Omos is currently a freelance web/application developer who is currently focused on developing Java enterprise applications with the Jakarta EE framework.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button