• Home
  • About
    • Thoughts To Pen photo

      Thoughts To Pen

      My thoughts on Computer Programming || Psychology || Personal Finances || & much more...

    • Learn More
    • Twitter
    • Instagram
    • Github
    • StackOverflow
  • Posts
    • All Posts
    • All Tags
  • Projects
  • Portfolio
  • Resources
  • About

Module Import Declarations (Java 25)

26 Sep 2025

Module Import Declarations (JEP 511)

How many lines of your Java files are just import statements? 10? 20? 50?

We’ve all been there:

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.function.Function;
// ... and on and on

Wildcard imports (import java.util.*) help, but they are limited to a single package. If you use classes from java.util, java.io, and java.net, you still need multiple lines.

Java 25 introduces Module Import Declarations (JEP 511), a powerful way to import everything a module exports with a single line.

The Power of import module

With this new feature, you can import all the public classes and interfaces from an entire module.

For example, to access everything in the core Java libraries (the java.base module), you can write:

import module java.base;

public class ModuleImportExample {
    public static void main(String[] args) {
        List<String> list = List.of("A", "B", "C"); // from java.util
        File file = new File("test.txt");           // from java.io
        Socket socket = new Socket();               // from java.net
    }
}

Wait, where are the imports for List, File, and Socket? They are all brought in by import module java.base;!

Since java.base contains packages like java.lang, java.io, java.net, java.util, and more, importing the module makes all of their public types available to your code.

Handling naming conflicts

Since you are importing so many classes, what if there is a conflict? For example, java.util.Date and java.sql.Date (if you imported java.sql).

Java handles this gracefully. If you use a class name that is ambiguous (exists in multiple imported packages), the compiler will give an error, and you simply need to explicitly import the specific class you want to resolve the ambiguity:

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

import java.sql.Date; // Resolve conflict for 'Date'

public class conflicts {
    Date sqlDate; // Uses java.sql.Date
}

Why use this?

  1. Simplicity: Especially for learning and prototyping, it removes the friction of hunting down packages.
  2. Readability: Reduces visual noise at the top of your files.
  3. Convenience: Great for “Java playgrounds” or scripts (especially when combined with Compact Source Files!).

This feature essentially brings the convenience of “import everything” that other languages enjoy, while keeping Java’s strong module system structure.



programmingjavajava25 Share Tweet Msg