How do I Use an Instance main Method in Java 25?

Java 25 finalizes JEP 512: Compact Source Files and Instance Main Methods, which was previewed in earlier JDK releases. This feature makes Java far more approachable for beginners and reduces boilerplate for small programs and scripts.

What Changed?

Traditionally, every Java program required this ceremony:

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

With Java 25, the main method no longer needs to be:

  • public
  • static
  • Declared with a String[] args parameter

The New Rules

The JVM launcher now looks for a main method in this order of preference:

  1. static void main(String[] args) — traditional form
  2. static void main() — no parameters
  3. void main(String[] args) — instance method with args
  4. void main() — instance method, no args simplest form

If an instance main method is found, the JVM will implicitly create an instance of the enclosing class using its no-argument constructor, then invoke main on it.

Example 1: The Simplest Instance main

public class Hello {
    void main() {
        System.out.println("Hello from an instance main!");
    }
}

That’s the entire program. No static, no String[] args, no public.

Run it with:

java Hello.java

Example 2: Instance main With Fields and Helper Methods

Because main is now an instance method, it can freely use instance fields and call other instance methods without needing static everywhere:

public class Greeter {
    private final String greeting = "Hello";

    void main() {
        greet("Java 25");
        greet("Developers");
    }

    void greet(String name) {
        System.out.println(greeting + ", " + name + "!");
    }
}

Notice that greet is also a plain instance method — no static modifier needed.

Example 3: Combined With Compact Source Files

Java 25 also allows you to omit the enclosing class entirely (Compact Source File):

void main() {
    System.out.println("No class declaration required!");
}

Save this as Demo.java and run:

java Demo.java

The compiler implicitly wraps the code in a synthetic class for you.

Example 4: Instance main With Arguments

If you still need command-line arguments, just declare them:

public class Echo {
    void main(String[] args) {
        for (String arg : args) {
            System.out.println("Argument: " + arg);
        }
    }
}

Requirements & Caveats

  • The enclosing class must have an accessible no-argument constructor (the default one is fine if you don’t declare any constructor).
  • The class cannot be abstract.
  • If both a static and an instance main exist, the static one wins (per the resolution order above).
  • The instance main method cannot be private. It must be at least package-private.
  • To run a single source file directly (java Foo.java), you don’t need to compile first — the launcher handles it.

Why This Matters

  • Lower barrier for beginners — no need to explain public, static, String[] args, or classes on day one.
  • Cleaner scripts — small utilities and experiments become much more concise.
  • Smoother learning curve — students can gradually introduce classes, static, and access modifiers as they progress, rather than all at once.

Quick Comparison

Style Java ≤ 20 Java 25
public static void main(String[] args) ✅ Required ✅ Still works
static void main() ❌ ✅
void main(String[] args) ❌ ✅
void main() ❌ ✅
No class declaration ❌ ✅ (Compact Source File)

Instance main methods, combined with compact source files, make Java 25 one of the most beginner-friendly releases in the language’s history — while remaining fully backward compatible with every existing Java program.

Leave a Reply

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