Learning Note: Java 21 Feature – Unnamed Classes and Instance Main Methods (Preview)

July 28, 2025 (4mo ago)

Jump to FAQs

In my ongoing study of Java 21, guided by the “Java 21 – Exploring the Latest Innovations for 2024” course, I’ve reached the section on one of the most beginner-friendly enhancements: unnamed classes and instance main methods. This feature is currently in preview, but it’s a significant step toward making Java more accessible, especially for newcomers.

Motivation: Reducing Verbosity for Beginners

Traditionally, writing a simple Java program (like “Hello, World”) requires a lot of boilerplate code: defining a class, writing public static void main(String[] args), and then adding the print statement. For beginners, this can be overwhelming and unnecessarily complex.

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

Java 21 aims to minimize this verbosity. The new feature allows you to write much simpler entry-point code, making the language more approachable for students and those new to Java.

What’s New: Minimal Entry Point

With this preview feature, you can now write a Java program like this:

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

Setting Up and Running an Unnamed Class in VSCode

If you’re using Visual Studio Code (VSCode), the process is straightforward:

  1. Create a New Java Project:

    • Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P).
    • Type and select “Create Java Project.”
    • Choose “No build tools” for simplicity.
    • Select a folder and provide a project name.
  2. Write Your Minimal Java Program:

    • In the src folder, create a file (e.g., App.java).
    • Write your code using the new minimal style.
  3. Compile and Run with Preview Features:

    • Open a terminal and navigate to your src directory.
    • Compile with preview enabled:
      javac --release 21 --enable-preview App.java
    • Run with preview enabled:
      java --enable-preview App
    • You should see your output (e.g., “Hello, Java 21!”).

Note: If you try to run this code without enabling preview features, it will not compile or run, as this feature is not yet standard.

Key Takeaways

This feature, although small, reflects Java’s ongoing effort to modernize and lower the entry barrier for new programmers. I find it a welcome change, especially as I continue to explore more of Java 21’s innovations in my learning journey.

Discuss this post:

Frequently Asked Questions

What are unnamed classes in Java 21?

Unnamed classes allow you to write Java programs without explicitly declaring a class. You can write a simple main() method directly without the traditional 'public class' boilerplate, making Java more beginner-friendly.

Why do I need to enable preview features to use unnamed classes?

Unnamed classes and instance main methods are currently a preview feature in Java 21, meaning they're not yet part of the standard language. You must use --enable-preview flag during compilation and execution to test these features.

Can I use unnamed classes in production applications?

No, preview features like unnamed classes should not be used in production code. They're experimental features that may change or be removed in future Java versions. Use them only for learning, prototyping, or experimentation.

What's the difference between the old main method and the new simplified version?

The traditional main method requires 'public static void main(String[] args)' inside a class declaration. The new version allows just 'void main()' without class declaration or public static modifiers, reducing boilerplate significantly.

Do I still need to create a .java file with a class name?

Yes, you still create a .java file (like App.java), but you don't need to declare a class inside it that matches the filename. The Java compiler handles the class creation automatically.

Will this replace the traditional way of writing Java programs?

No, this feature is designed to complement traditional Java programming, especially for beginners and simple programs. Complex applications will still benefit from the full object-oriented structure that traditional class declarations provide.

Can I use other Java features like imports and methods with unnamed classes?

Yes, you can still use imports, define additional methods, and use most Java features. The unnamed class feature primarily simplifies the entry point, not the entire language structure.