JEP445 Instance main() methods coming for Java 21 LTS

Java 21 is only a few weeks away from release. One of the changes that I think is the most interesting and I’m surprised that it’s taken this long to be included is JEP445 “Unnamed Classes and Instance main methods”. The goals of this JEP explain precisely the benefit of this change:

“Offer a smooth on-ramp to Java so that educators can introduce programming concepts in a gradual manner.”

“Reduce the ceremony of writing simple programs such as scripts and command-line utilities.”

“Instance main methods” refers to a change to the language requirement that the single entry point to a Java application is the main() method with this exact signature:

public void main(String[] args) {
    ...
}

…which is a lot to unpack to understand how to get started even with the most basic “Hello World” app. To explain this signature to brand new developers learning Java, I usually see the required main signature explained away with “ignore why we need this for now, just accept that you need to define the main method exactly like this for it to work” which doesn’t exactly give much comfort.

JEP445 Instance main methods allows support for much simpler main methods without the required scope, return type and params:

void main() {
...
}

To make it even easier, since this new support now allows you to use an instance method instead of the previously required Class method (static), you can call other instance methods without instantiating the class, and without understanding why you can’t call instance methods from a static method. Prevously you need to instantiate your Class first, before calling instance methods like this:

public class PreJava21App {
    public String getGreeting() {
        return "Hello World!";
    }

    public static void main(String[] args) {
        System.out.println(new PreJava21App().getGreeting());
    }
}

With JEP445 now you can code a simpler main() like this:

public class Java21App {
    public String getGreeting() {
        return "Hello World from Java 21!";
    }

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

The next change on this JEP with Java 21 introduces Unnamed Classes, so now even the Class declaration can be implicit, like this:

    public String getGreeting() {
        return "Hello World from Java 21 with implicit Classes!";
    }

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

Under the covers there’s still a Class, it’s implicitly named the same as the filename, so if you save the above in a file classed Java21App, then you’d run it with ‘java Java21App’.

To enable these features currently (as of OpenJDK build 21-ea+28), you need to pass the ‘–enable-preview’ param. To pass this with gradle, add the following to your build.gradle:

//pass required JVM param for compile time
tasks.withType(JavaCompile) {
    options.compilerArgs += "--enable-preview"
}

//pass required JVM param for runtime
tasks.withType(JavaExec) {
    jvmArgs += '--enable-preview'
}

Programming Languages are tools. Learning a tool doesn’t make you a developer – more analogies

In Reddit programming subreddits there are never ending questions about how to learn a programming language to get rich, or how to learn language XYZ because ‘I need to get a job next month’. If the people posting these questions did only a few minutes of research by reading previous similar questions (you don’t have to scroll back too far before you’ll find another similar question) they’d realize how unrealistic their expectations are.

Learning a programming language doesn’t make you a developer. The analogy I like to use to answer these questions is that learning to use a hammer by itself does not mean you can build a timber framed house. I summarized this before in this post:

I have another analogy that maybe is more relatable given that some people are attracted to the Gold Rush of software development. Doctors make a lot of money. If you learn how to slice open someone’s chest to do open heart surgery then you’ll be able to get a job as a surgeon right? If course not, that’s not how it works. Similarly, learning a programming language by itself will not help you get a job as a software developer.

As a new developer, when does problem solving start to get easier?

A common question asked by new developers starting out, especially during the first few months of learning, is ‘when does it start to get easier?’

The reality is it never really gets ‘easy’, but with experience it does get easier. When you’ve seen enough similar problems and previously worked out solutions before, that knowledge and experience helps you approach new problems.

A completely new problem that you’ve never seen before obviously takes more time and effort to work out an appropriate solution. A problem that you’ve already seen before takes less time because you’re able to apply the previous knowledge and solution.

The point where things start to get easier is where you’re able to pull from previous similar problems that are different from the current problem you’re working on, but enough knowledge and previous experience helps to find a solution to a completely new problem.

When do you reach this point? It varies for everyone, it depends what you’re working on. If you’re working in an environment where you’re working on very similar problems day after day, you’re not encountering enough variety to grow your experience so will take you longer. If you’re lucky enough to be working in an environment when you’re exposed to a broad range of problem types, then you’ll develop your experience quicker.

What tech stack was I working with in May 2003?

Clearing out some stuff in the home office I came across this CD-ROM of tech tools, products and libraries that I was using at work at this time:

  • JBoss – the open source Java EE app server of choice at this time
  • Xerces – Java XML Parsing library (DOM)
  • Amazon WS Kit – not sure what this was, maybe an SDK for AWS?
  • Apache – HTTP web server
  • Caucho Resin – no longer around – if I remember right this was an optimized Servlet runtime
  • EJ Tech Profiler – I think this was a competitor to JProfiler?
  • Homesite – HTML editor
  • IBM WS Kit – SDK for Java XML webservices
  • IntelliJ – still around today. At the time I don’t think it was as popular as Eclipse or Netbeans, but is probably the best. Java IDE around today

Interesting look back at Java versions in use at the time:

  • Java JDK 1.4 (at the time, also called Java 2 after JDK version 1.2, abbreviated as J2SE)
  • Java EE 1.2 and 1.3 – abbreviated to J2EE – the days when J2EE was actually called J2EE before dropping the 2 and just becoming Java EE after Java EE 5 was released in 2006)