Your Privacy

This site uses cookies to enhance your browsing experience and deliver personalized content. By continuing to use this site, you consent to our use of cookies.
COOKIE POLICY

Skip to main content

Java 9 | When to Use Modules

Java 9 | When to Use Modules
Back to insights

The release of Java 9 added strong encapsulation to the module system. This seemingly small addition has major implications for improving software development efforts – the module system offers additional support for SOLID development.

When to Use Java Modules

There are a couple of reasons why it makes sense to choose modules during development. Many Java development houses select Spring or Spring Boot just to satisfy the “D” in SOLID development. If a Java project only requires Dependency Injection, Java 9 can do this without any additional frameworks. Spring, and occasionally Spring Boot, require configuration files to map Java class implementation to interfaces. Creating these configuration files can be tedious. Modules do not require the same configuration files, which saves development time and, therefore, money. Modules also support the “S” in SOLID development. Software developers can decompose a monolithic Java application into many single-purpose modules. Properly architected applications using SOLID principles and modules can yield applications that are easier to maintain than if the application were a monolith, which again saves money.

Code Example

If you’d like to get started with an example implementation, download the code located on Github here. Edit the build script variables as necessary to match your file system and then run it. The output of the main class will display. Notice that the main class only imports interfaces Coffee and CoffeeService to achieve its goal. Further inspection shows that interfaces are encapsulated inside of their own modules, com.example.coffee and com.example.coffeeservice. Implementation files are in different modules, com.example.coffee.impl and com.example.coffeeservice.impl.

Modularization requires a module-info.java file for each module. Compare the module-info.java files inside of com.example.coffee and com.example.coffee.impl for guidance on the visibility keywords required for separating interfaces from implementation.

It is important to point out the ServiceLoader class in the main class. The ServiceLoader is the class responsible for resolving the implementation files. Given an interface, the ServiceLoader can return an Iterable containing the files that implement the interface. The desired implementation is selected from the Iterable.

Conclusion

It makes sense to select Java modules over Spring or Spring Boot if you just need a lightweight system for SOLID development. UDig has a crew of excellent Java developers that are skilled in everything from Spring Boot to Adobe Experience Manager and can be a great resource for your next Java project.

About Cody Halbleib

Cody Halbleib is a Senior Consultant on the Software team. His family is his partner, Jamie, and his Border Collie mix, Walter.

Digging In

  • Digital Products

    What to Ask in a UX Discovery Session

    Better questions lead to better design. In my years as a UI/UX designer, I’ve learned that many design challenges may come from misalignment at the start. Discovery sessions are where product clarity is either made or missed. These are a few discovery questions I consistently return to in the discovery phase of my UX design […]

  • Digital Products

    Designing For Play and Friction in a Fast-Paced World

    At UDig, play is an integral part of our philosophy. “Having fun” is embedded in our culture, and we always find opportunities to use play to encourage collaboration, ignite creativity, and make room for bold experimentation to build stronger teams and solve problems ranging from the seemingly simple to the most complex. I always have […]

  • Digital Products

    Config 2025 Day Two Recap

    It felt as though Config 2025 ended as soon as it began, and I believe those of us that attended are all the better for it. By the end of the day, various inspirational and informative talks had been given by thought leaders and innovators in the product space. Between sessions, we had the opportunity […]

  • Digital Products

    Inside Config 2025: What’s New in Figma

    Config 2025 kicked off with a bang on Day 1. Figma’s annual conference brings together designers, developers, and all those involved in the making of a product. The 2-day event has a stacked lineup of accomplished speakers ready to share their insights on the world of product building. At today’s opening keynote, the Figma team, […]

  • Digital Products

    Choosing the Right Modernization Approach

    When organizations decide it’s time to modernize their technology infrastructure, choosing the right approach is crucial. Modernization isn’t merely a technical upgrade; it’s a strategic business move that significantly impacts growth, agility, and long-term success. Here’s how your company can effectively begin to select the best modernization strategy tailored to your goals and challenges. In […]

  • Digital Products

    TAG Panel: Differentiate Your Customer Experience

    Join the CX and Product Management Societies to hear from our panel of Human-Centered Design experts on the business value of Agentic AI.