When should you prefer module-info

When developing Java applications, you should prefer using module-info.java in the following situations:

  • When your application has multiple modules that need clear dependencies and interfaces.
  • When you want to take advantage of strong encapsulation, making your classes less accessible than before.
  • When you aim for better maintainability and easier version management in larger codebases.
  • When you want to use Java's module system introduced in Java 9, which helps to reduce classpath issues.

Here's an example of a simple module-info.java file:

module com.example.myapp { requires com.example.library; exports com.example.myapp.services; }

Java module-info modular programming Java 9 dependencies encapsulation maintainability version management