CS 261 University of Puget Sound Spring, 2020
 
Computer Science II
Abstract Data Types and their Implementations, Some Basic Algorithms,
Object-oriented Problem Solving, and Efficiency
 

Warning: This course is under development. Although the basic structure of this course is largely established, nothing on this Web site should be considered official or even possibly correct.
DO NOT MAKE PLANS BASED ON THE CONTENTS OF THIS SITE UNTIL JANUARY, 2020.

Java Packages and Modules: A Brief Introduction

Summary

Historically, packages in Java provide a mechanism to group related classes together.

As Java applications have gotten larger over time, software developers have found that package permissions provide only coarse control of data and methods. If one [external] package is granted access, then all packages enjoy the same access. Java modules, new in Java Version 9, allow more detailed permissions—which package/class can directly utilize elements within another package.

Introduction

Two vital components of large-scale software development involve

Encapsulation

Within computer science, the first component above is called encapsulation. This principle has at least four motivations:

Java Classes

Within Java, a class provides a basic mechanism for bringing data and operations within a single unit. Within this framework, a class can control what data and operations may be accessed outside the class, and what data and operations are available for internal use only.

For example, a common practice specifies that data fields should always be private. This allows a class to store data in whatever way seems helpful, and the underlying representation can change over time. If one representation of data turns out to have undesired limitations, then the representation could be changed, getters and settings might be updated, but all external applications could use the low-level class as is.

By convention, Java class names start with a capital letter and designate a collection of data elements and methods.

Java Packages

A Java package is a mechanism for gathering several classes within a well-defined collection. For example, a library might have book classes, a catalog class, and a user class. Each of these classes has its own data and operations, and a package may clarify which data elements and operations may be used by others in the package. In this context, these classes are considered to be related to each other, and each class might begin with a statement

  package library;

to designate that books, catalogs, and users may be closely connected.

Similarly, any program wishing to use a class in the library package might begin with a statement

  import library;

Further, within a Java class, data fields and methods are public, private, or protected, and (as we shall discuss later in the course), these terms provide certain access within a package.

In contrast, the terms public, private, and protected generally provide somewhat more restricted classes outside outside a package.

Overall, for many versions of Java (up through version 8), the distinction of classes within a package versus classes outside a package allowed reasonable control over what class could access what material.

Java Modules

However, as software applications have increased in size, programmers realized a need for finer control for access to data and/or methods. Control within a package could be specified reasonably, but different packages should be given different levels of access. Further, the management of large programs could be aided by explicitly specifying what classes or packages were required at each stage and by identifying what classes or packages were allowed to use what.

Starting with Java, version 9, the concept of a module was introduced to provide this added level of access control. In particular,

Although modules provide considerable control over what packages and classes can be utilized where, simple programs (e.g., those at the start of this course) likely will involve only a few classes, located within a single package, which in turn is placed within a single module. In this situation, module-info.java files still should be present (e.g., when defining a project in the eclipse programming environment), but this module-info.java file may be an empty shell:

  module examples {

  }

Some Additional Reading

This current reading is intended to provide a general overview of the notion of packages and modules in Java. An interested reader might search the Web for additional information, such as Java 9 Modules (Part 1): Introduction by DZone.


created 7 September 2019 by Henry M. Walker
last revised 11 September 2019
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.