Ease into Java 21: Uncovering Instance Main Methods with JEP 445

Photo of Luqman Saeed by Luqman Saeed

In the world of programming, stepping stones are crucial for novices to transition into proficient developers. This journey often begins with understanding the syntax and semantics of a given programming language. Java, being one of the popular programming languages, has always aimed to be an effective medium for both novices and experienced developers. The recent release of Java 21 introduced a core feature, known as Unnamed Classes and Instance Main Methods through JEP 445, aimed at simplifying the learning curve for new programmers, making it easier for them to write their first program without much fanfare and verbosity.

The objective of JEP 445 is to provide a "smooth on-ramp to Java, thereby allowing educators to introduce programming concepts in a gradual manner. It aims to help students write basic programs in a more concise way and grow their code gracefully as their skills advance". One of the notable changes is the reduction in ceremonial, boilerplate code when writing simple programs such as scripts and command-line utilities, without the need to introduce a separate beginner's dialect of Java or a different toolchain. Program writing using the newly introduced constructs compile and run using existing Java tools and utilities.

In the conventional approach, a simple 'Hello, World!' program in Java requires the declaration of a class and a static main method. For instance:

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

The above code, though simple, introduces several "heavyweight" Java language mechanics that can serve as cognitive barriers to understanding the essence of the code; in this case, print the string "Hello, World!" to the console. JEP 445 allows for simpler entry points into the language. We can rewrite the above code as follows.

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

The above code is significantly shorter, simpler and readable, even for a beginner. It shows the use of the features in JEP 445 in action. The whole class declaration is gone, now made implicit. The main method is stripped of all the ceremony, leaving just its name and return type. Even its visibility is gone. This is an instance main method in an unnamed class. 

JEP 445 is another important step towards simplifying Java for beginners. It's also a giant leap towards making Java more accessible and less intimidating for everyone willing to learn it. As newcomers transition from writing simple programs to more complex ones, the skills and concepts learned remain applicable, making the journey of learning Java a progressive and rewarding experience.

It's important to note that JEP 445 is a preview feature in Java 21, which means it needs to be explicitly enabled when using JDK 21. You can do this by compiling the program with the --enable-preview flag​. As a preview feature, it is likely to change in its final release. Keep in mind when adopting all preview features.

Starting out with Java? Choose Payara Community and Jakarta EE to investigate Java SE extended for enterprises. Here are some resources to get you started:

 

Related Posts

Comments