Java Packages and Modules: Organizing Code & JPMS Architecture

As software grows in size, organizing hundreds of source files into logical namespaces is critical. In Java, Packages provide directory-based namespace encapsulation, while the Java Platform Module System (JPMS) provides modular boundaries across packages.

1. Creating and Declaring a Package

To assign a class to a package, place the package statement at the very top of the .java file:

// Must be the first non-comment line in the source file:
package com.veewom.banking.services;

public class TransactionService {
    public void processTransfer() {
        System.out.println("Processing bank transfer...");
    }
}

2. Importing Classes from Other Packages

To use a class residing in another package, import it explicitly:

// Explicit Single Class Import (Recommended for clarity):
import com.veewom.banking.services.TransactionService;

// Wildcard Package Import (Imports all classes in package):
import java.util.*;

public class AppLauncher {
    public static void main(String[] args) {
        TransactionService service = new TransactionService();
        service.processTransfer();
    }
}

3. Introduction to Java 9+ Modules (JPMS / Project Jigsaw)

JPMS enforces Strong Encapsulation. Even if a class is declared public, it is completely hidden from external modules unless its package is explicitly exported in module-info.java:

// module-info.java (Root of module source directory)
module com.veewom.banking {
    // Requires dependent modules
    requires java.sql;
    requires org.apache.commons.lang3;

    // Exports specific packages to consumers
    exports com.veewom.banking.services;
    exports com.veewom.banking.models;
}

University Exam Corner (BCA / B.Sc CS)

Exam Question: "Which package in Java is imported automatically into every source file without an explicit import statement?"

Answer: java.lang! The Java compiler automatically imports java.lang.* into every single Java file. This is why fundamental classes like System, String, Math, Thread, Object, and Integer can be used without any import statements.

Next Step

Now let us explore modern Java Records—the concise, immutable data carriers introduced in Java 16+. Proceed to: Java Record Classes (Java 16+) →

Frequently Asked Questions (FAQ)

Q1. What is a Package in Java and why is it used?
A package is a named namespace that groups related classes, interfaces, and sub-packages together into a folder directory structure. It prevents class naming collisions (e.g. having two Date classes) and controls access levels via package-private scope.
Q2. What is the CLASSPATH environment variable?
CLASSPATH tells the Java compiler (javac) and JVM (java) where on the filesystem or inside JAR archives to look for compiled .class files.
Q3. What is the Java Platform Module System (JPMS)?
Introduced in Java 9 (Project Jigsaw), JPMS adds a higher architectural layer above packages. A module is a uniquely named bundle of packages configured via 'module-info.java', explicitly declaring which packages it exports to outside modules and which external modules it requires.

Explore Related Java Tutorials

Accelerate your Java mastery by studying these tightly connected core tutorials:

Access Modifiers →

Package-private default access.

Java Syntax Rules →

Package declaration rules.

Java Records →

Packaging modern data records.

JDK 21 Setup →

Configuring CLASSPATH.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!