Creating a Module Dependency - Java Object Oriented Design

Java examples for Object Oriented Design:Module

Introduction

Specify the dependency within the module descriptor.

Create the module org.secondModule by creating a new directory within the src directory.

Create a .java file named module-info.java and place it into that location.

The contents of the module descriptor should look as follows:

module org.secondModule { 
    exports org.secondModule; 
} 

The second module makes sources contained within the org.secondModule package available to other modules that require it.

The sources for the module should be placed into a class named Calculator.java, and this file should be placed into the src/org.secondModule/org/secondModule directory.

package org.secondModule; 
import java.math.BigDecimal; 
public class Calculator { 
    public static BigDecimal calculateRate(BigDecimal days, BigDecimal rate) { 
        return days.multiply(rate); 
    } 
} 

Another org.firstModule uses of org.secondModule as follows:

package org.firstModule; 
import org.secondModule.Calculator; 
import java.math.BigDecimal; 
public class Main { 
    public static void main(String[] args) { 
        System.out.println(Calculator.calculateRate( BigDecimal.TEN, new BigDecimal(22.95) 
        )); 
    } 
} 

The module descriptor for org.firstModule must be modified to require the dependency:

module org.firstModule { 
    requires org.secondModule; 
} 

To compile the modules, specify the javac command, using a wildcard to compile all code within the src directory:

javac -d mods --module-source-path src $(find src -name "*.java")

Related Tutorials