DEV Community

Munaf Badarpura
Munaf Badarpura

Posted on

Exploring Java 25: New Features with Example Code

Java continues to evolve rapidly with its six-month release cycle, and Java 25, released on September 16, 2025, and it brings a lot of enhancements aimed at simplifying development, especially for beginners.

And for advanced users, Java 25 introduced powerful tools. This version focuses on reducing boilerplate code, improving readability, and enhancing performance in concurrent scenarios.

In this blog, we'll dive into the key new features of Java 25 along with practical examples so it makes more sense how they work. Whether you are a new programmer or an experienced developer, these updates make Java more accessible and efficient.

1. Instance Main Methods

One of the most beginner-friendly changes in Java 25 is the support for instance main methods. Traditionally, the entry point for Java programs required a public static void main(String[] args) method. Now, you can omit public, static, and even the String[] args parameter if they're not needed. This simplifies writing quick test programs or scripts.

The JVM follows a specific order to locate the main method:

  1. It first checks for the standard public static void main(String[] args).
  2. If not found, it looks for public static void main().
  3. If only an instance (non-static) main exists, it instantiates the class using a public no-arg constructor and calls the method.
  4. If no valid main is found, it throws an error.

Example

class Test {
    void main() {
        System.out.println("Hello, World!");
        System.out.println("This is Coding Shuttle");
    }
}
Enter fullscreen mode Exit fullscreen mode

In this case, since there's no static main, the JVM creates an instance of Test and invokes the instance main() method.

2. Compact Source Files (Unnamed Classes)

Java 25 introduces compact source files, where you don't need to explicitly declare a class. The compiler implicitly wraps your code in an unnamed, final class with a default no-arg constructor. This is perfect for simple scripts or learning exercises, as it lets you focus on logic without class declarations. However, a launchable main method is mandatory; otherwise, compilation fails.

You can define methods, fields, and use this within the file.

Example: Basic Hello World

void main() {
    System.out.println("Hello, World");
}
Enter fullscreen mode Exit fullscreen mode

Example: With Fields and Methods

String msg = "Hello, World";

void main() {
    printMsg();
}

void printMsg() {
    System.out.println(msg);
}
Enter fullscreen mode Exit fullscreen mode

This code runs as if it's inside an implicit class. The generated class is final and can't be extended or referenced by name.

3. Simplifying Console Input/Output with the IO Helper Class

Console I/O in Java has historically been verbose, requiring classes like BufferedReader and exception handling. Java 25 addresses this with the new java.lang.IO class, providing static methods for simple printing and reading. Since it's in java.lang, no import is needed (though static imports can be used for convenience).

Key methods:

  • IO.print("text"): Prints without a newline.
  • IO.println("text"): Prints with a newline.
  • IO.println(): Prints a blank line.
  • IO.readln(): Reads a line of input.
  • IO.readln("Prompt: "): Prompts and reads input.

Example: Printing

import static java.lang.IO.*;

void main() {
    println("Hello, Coding Shuttle!");
}
Enter fullscreen mode Exit fullscreen mode

Example: Input

import static java.lang.IO.*;

void main() {
    var name = readln("What's your name? ");
    println("My Name is, " + name);
}
Enter fullscreen mode Exit fullscreen mode

This eliminates boilerplate, making interactive programs easier for beginners.

4. Automatic Imports of Common APIs

In compact source files, Java 25 automatically makes classes from commonly used packages in java.base (e.g., java.util, java.io) available without explicit imports. This reduces setup time for simple programs.

Example

void main() {
    IO.println("Hello, World!");  // no import needed here
}
Enter fullscreen mode Exit fullscreen mode

Here, IO is accessible directly, streamlining code in unnamed classes.

5. Flexible Constructor Bodies

Constructors in prior Java versions required super(...) or this(...) calls as the first statement. Java 25 relaxes this, allowing statements before these calls in the "prologue" section. However, the prologue can't reference this or instance members (except to initialize uninitialized fields). The "epilogue" (after the call) follows normal rules.

Example

class Parent {
    Parent() {
        IO.println("Parent Class Constructor");
    }
}

class Child extends Parent {
    Child() {
        IO.println("Child Class Constructor");
        super();
    }

    public static void main(String[] args) {
        new Child();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Child Class Constructor
Parent Class Constructor
Enter fullscreen mode Exit fullscreen mode

This feature enables pre-initialization logic, like logging or computations, before calling the superclass constructor.

6. Module Import Declarations

For modular applications, Java 25 adds import module M;, which imports all packages exported by module M. This replaces multiple per-package imports, simplifying code when using libraries like java.sql.

Before

import java.util.*;
import java.sql.*;
import java.nio.file.*;
import javax.sql.*;

public class DataProcessor {
    Path filePath = java.nio.file.Paths.get("data.txt");
    Map<String, Integer> dataMap = new HashMap<>();
    Connection dbConn = DriverManager.getConnection(...);
}
Enter fullscreen mode Exit fullscreen mode

After

import module java.base;
import module java.sql;

public class DataProcessor {
    Path filePath = Paths.get("data.txt");        // from java.base
    Map<String, Integer> dataMap = new HashMap<>(); // from java.base
    Connection dbConn = DriverManager.getConnection(...); // from java.sql
}
Enter fullscreen mode Exit fullscreen mode

This reduces import clutter and improves maintainability.

Read complete blog here : https://www.codingshuttle.com/blogs/what-s-new-in-java-25-top-features-examples-and-enhancements-for-beginners-and-pros/

Top comments (0)