Modules
- 100% developed as of Dec 31, 2012 Statements
- 100% developed as of Mar 10, 2013 Conditional blocks
- 100% developed as of Mar 10, 2013 Loop blocks
- 100% developed as of May 24, 2013 Boolean expressions
- 100% developed as of Feb 16, 2010 Variables
- 100% developed as of Mar 10, 2013 Primitive Types
- 100% developed as of Mar 10, 2013 Arithmetic expressions
- 100% developed as of May 24, 2013 Literals
- 100% developed as of Mar 10, 2013 Methods
- 100% developed as of May 24, 2013 String
- 100% developed as of Mar 10, 2013 Objects
- 100% developed as of Jul 5, 2012 Packages
- 25% developed as of Sep 21, 2025 Modules
- 100% developed as of Mar 10, 2013 Arrays
- 75% developed as of Jan 11, 2013 Mathematical functions
- 75% developed as of Jan 11, 2013 Large numbers
- 75% developed as of Jan 11, 2013 Random numbers
- 100% developed as of Apr 8, 2013 Unicode
- 100% developed as of Apr 8, 2013 Comments
- 100% developed as of Sep 27, 2007 Keywords
- 100% developed as of Aug 6, 2013 Coding conventions
- 0% developed as of Mar 26, 2018 Lambda expressions
Since Java 9, one can use modules to better encapsulate code. The Java Platform Module System (JPMS) is used to specify distribution of a collection of packages.
A module be used to specify a collection of packages belonging to the public API of a Java program. This is primarily useful for a library, which may contain classes intended for internal use.
The module declaration is placed in a file named module-info.java at the root of the module’s source-file hierarchy. The JDK will verify dependencies and interactions between modules both at compile-time and runtime.
For example, the following module declaration declares that the module com.foo.bar depends on another com.foo.baz module, and exports the following packages: com.foo.bar.alpha and com.foo.bar.beta:
module com.foo.bar { requires com.foo.baz; exports com.foo.bar.alpha; exports com.foo.bar.beta; }
Java modules use the following keywords:
exports
: used in a module declaration to specify which packages are available to other modulesmodule
: declares a moduleopen
: indicates that all classes in a package are accessible via reflection by other modulesopens
: used to open a specific package for reflection to other modulesprovides
: used to declare that a module provides an implementation of a service interfacerequires
: used in a module declaration to specify that the module depends on another moduleto
: used with theopens
directive to specify which module is allowed to reflectively access the packagetransitive
: used with therequires
directive to indicate that a module not only requires another module but also makes that module's dependencies available to modules that depend on ituses
: used in a module to declare that the module is using a service (i.e. it will consume a service provided by other modules)with
: used with theprovides
directive to specify which implementation of a service is provided by the module
As of Java 25, modules can themselves be imported, automatically importing all exported packages. This is done using import module
. For example, importmodulejava.sql;
is equivalent to
importjava.sql.*; importjavax.sql.*; // Remaining indirect exports from java.logging, java.transaction.xa, and java.xml
Similarly, importmodulejava.base;
, similarly, imports all 54 packages belonging to java.base
.
importmodulejava.base; /** * Importing module java.base allows us to avoid manually importing most classes * The following classes (outside of java.lang) are used: * - java.text.MessageFormat * - java.util.Date * - java.util.List * - java.util.concurrent.ThreadLocalRandom */ publicclass Example{ publicstaticvoidmain(String[]args){ List<String>colours=List.of("Red","Orange","Yellow","Green","Blue","Indigo","Violet"); IO.println(MessageFormat.format("My favourite colour is {0} and today is {1,date,long}", colours.get(ThreadLocalRandom.current().nextInt(colours.size())), newDate() )); } }