Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

DevNation Live Blog: Java 9 modularity in action

June 28, 2016
Salem Elrahal
Related topics:
Java

    Common to all solid architecture design is the notion of encapsulation, hiding the internals of a component and exposing a strict contract for consumers. This allows implementation changes to happen while giving strong guarantees about the behavior of other components. At DevNation 2016 Sander Mak and Paul Bakker from Luminis Technologies presented on the upcoming modularity features in Java 9 provided by the Jigsaw JSR which will introduce native mechanisms for enforcing strong encapsulation and service abstraction in the Java language. Java modules are a way of grouping java files and defining what services they provide and consume.

    You might think "Hey, Java class files are typically distributed in JAR files, doesn't that count for modularity?" or maybe even "What about OSGi? Isn't that modularity for the JVM.". Although JAR files look like modules during compile time they get clumped together during runtime and there are no "fences" between the classes to hide implementation details. OSGi is a great modular framework but suffers from narrow adoption and adding modularity to the JDK will provide those same encapsulation benefits to many libraries and applications. Furthermore part of the Java 9 specification is targeting modularizing the JDK itself, which cannot be done with OSGi (a framework built on top of the JVM).

    Stronger encapsulation means hiding whole packages that are implementation specific from consumers of your API and these principles apply to the JDK as well as other libraries. Currently implementation specific code (like classes from the packages com.sun.* and jdk.internal.* from the JDK) ideally should not be used but that doesn't stop developers. Those fortunate developers among us tasked with maintaining existing legacy systems might at this point question these fundamental changes and how they will impact existing code which could be using JDK internals. For now, the modularity refactoring in Java 9 will include exposing the internal unsafe classes to maintain backwards compatibility but will be provided with tooling to help migrate your legacy code. Automatic modules is one of these tools that will take your existing code and partially migrate it to modules. A plain JAR on the module path is an Automatic Module which will export everything and reads all other modules.

    Theory is great, lets talk details. Say we are using a module that exposes some Java interfaces, like a good encapsulated library, how do we get a reference to an Implementation of the interface? Including the concrete type directly will necessitate a compile time visibility to the implementation which is the exact type of dependency we are trying to avoid with modularity. Enter the ServiceRegistry. The Java modules system utilizes a services loader which has been around for quite a while but is getting a face-lift for new usage in Java 9. Using just a reference to the interface a Java class can look up implementations via the ServiceLoader and then call any methods.

    Iterator serviceImplentations = ServiceLoader.load(MyService.class);
    

    The service loader will obey your Module metadata, so your module must be explicit about needing an implementation of the MyService interface:

    module myApp {
     requires thirdPartyAPI;
     uses com.api.MyService
    }
    

    Likewise, part of the benefit of modularity lying in the requirement to enumerate your exposed services, your app will need to include a dependency on a module that provides an implementation:

    module ThirdPartyProvider {
     requires thirdPartyAPI;
     provides com.api.MyService with myProvider.MyServiceImpl
    }
    

    Now, the service loader will be able to find the concrete implementation and you can actually Get Stuff Done. Modularization as a first class feature within the JVM, how cool! Look for all these features and more coming in Java 9!

    Recent Posts

    • Red Hat Hardened Images: Top 5 benefits for software developers

    • How EvalHub manages two-layer Kubernetes control planes

    • Tekton joins the CNCF as an incubating project

    • Federated identity across the hybrid cloud using zero trust workload identity manager

    • Confidential virtual machine storage attack scenarios

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.