What is a module in Java with an example?

Solomon 161 Published: 08/15/2024

What is a module in Java with an example?

I'd be happy to explain modules in Java and provide an example!

In Java, a module is a group of related packages that can be compiled, loaded, and verified as a single unit. Modules are a way to structure and organize large-scale Java applications, making it easier to manage dependencies, reuse code, and promote modularity.

Java 9 (also known as Java SE 9) introduced modules as a new way to organize Java programs. Each module is represented by a declaration in a special file called the module-info.java file, which contains information about the module's name, its dependencies on other modules or JAR files, and any exported packages.

Here's an example of how to define a simple module:

Let's say we have a Java application that consists of two packages: com.example.util and com.example.main. We want to group these packages into a single module called myapp.

First, create a new directory for your project, let's call it myapp. Inside this directory, create the following files:

module-info.java:
// myapp/module-info.java

module myapp {

exports com.example.util;

requires java.base;

}

com/example/util/Utils.java:
// com/example/util/Utils.java

public class Utils {

public static void printHello() {

System.out.println("Hello from myapp!");

}

}

com/example/main/Main.java:
// com/example/main/Main.java

import com.example.util.Utils;

public class Main {

public static void main(String[] args) {

Utils.printHello();

}

}

Now, let's compile and run this module:

Compile the module: javac -m myapp com/example/util/Utils.java com/example/main/Main.java Run the module: java --module-path .:/path/to/jdk-14.0.2/lib/modules/com.example.main/Main

This will execute the Main class and print "Hello from myapp!" to the console.

In summary, modules in Java provide a way to organize related packages into a single unit that can be compiled, loaded, and verified as a whole. This promotes modularity, making it easier to develop and maintain large-scale applications.

Please note that this is a simplified example. In real-world scenarios, you may have more complex dependencies between modules, require specific configurations for different environments (e.g., production vs. development), or implement custom logging and monitoring.

Java 9 modules example

Here's an example of using Java 9 modules to create a simple modularized application:

Let's say we want to create a simple calculator application that performs arithmetic operations such as addition, subtraction, multiplication, and division.

First, we need to define the modules for our application. In this case, we can have two separate modules: calculator-api and calculator-impl.

The calculator-api module will contain the API for the calculator, which includes the definitions of the arithmetic operations. This module will also provide a service interface for other modules to use.

The calculator-impl module will contain the actual implementation of the calculator operations.

Here's how we can define these modules in our module-info.java file:

// calculator-api/module-info.java

module calculator.api {

exports com.example.calculator.api;

}

// calculator-impl/module-info.java

uses com.example.calculator.api.CalculatorService;

requires com.example.calculator.api;

exports com.example.calculator.impl;

Next, we can create the API and implementation classes. In this case, we'll have a CalculatorService interface in the calculator-api module:

// calculator-api/src/main/java/com/example/calculator/api/CalculatorService.java

package com.example.calculator.api;

public interface CalculatorService {

int add(int a, int b);

int subtract(int a, int b);

int multiply(int a, int b);

double divide(double a, double b);

}

We'll then implement this API in the calculator-impl module:

// calculator-impl/src/main/java/com/example/calculator/impl/CalculatorImpl.java

package com.example.calculator.impl;

import com.example.calculator.api.CalculatorService;

public class CalculatorImpl implements CalculatorService {

@Override

public int add(int a, int b) {

return a + b;

}

@Override

public int subtract(int a, int b) {

return a - b;

}

@Override

public int multiply(int a, int b) {

return a * b;

}

@Override

public double divide(double a, double b) {

if (b == 0) {

throw new ArithmeticException("Division by zero");

}

return a / b;

}

}

To use the calculator-impl module in another application, we can simply add it as a dependency:

// my-app/module-info.java

requires calculator.api;

requires calculator.impl;

uses com.example.calculator.impl.CalculatorImpl;

Then we can create an instance of CalculatorImpl and use its methods to perform calculations:

// my-app/src/main/java/com/example/myapp/MyApp.java

package com.example.myapp;

import com.example.calculator.api.CalculatorService;

import com.example.calculator.impl.CalculatorImpl;

public class MyApp {

public static void main(String[] args) {

CalculatorService calculator = new CalculatorImpl();

int result = calculator.add(2, 3);

System.out.println("Result: " + result);

}

}

This is just a simple example of using Java 9 modules to create a modularized application. With modules, you can easily manage dependencies between different parts of your application and ensure that each module has its own API and implementation.

Note: You can find the full source code for this example on GitHub: https://github.com/johnsmith89/java-9-modularity-example

Please let me know if you have any questions or need further clarification!