What is the difference between a package and a module in Java?

Java Packages and Modules

1. Short Answer

Packages and modules in Java serve different purposes in organizing code:

  • Package: A namespace mechanism for organizing related classes and interfaces. It provides a way to group related types and control access.
  • Module: A collection of packages with a module descriptor (module-info.java) that explicitly defines dependencies and exports. Introduced in Java 9 as part of the Java Platform Module System (JPMS).

2. Basic Concepts

Understanding the fundamental differences between packages and modules is crucial for modern Java development.

2.1 Packages

Packages are the traditional way of organizing Java code:

// Package declaration
package com.techoral.model;

// Class within the package
public class User {
    private String name;
    private String email;
    
    // Constructor and methods
    public User(String name, String email) {
        this.name = name;
        this.email = email;
    }
    
    // Getters and setters
}

Key characteristics of packages:

  • Organize related classes and interfaces
  • Provide namespace management
  • Control access through access modifiers
  • Follow hierarchical naming convention
  • Exist in all Java versions
  • No explicit dependency management
Note

Packages are primarily used for organizing code and preventing naming conflicts, while modules provide stronger encapsulation and dependency management.

2.2 Modules

Modules were introduced in Java 9 as part of the Java Platform Module System (JPMS):

// module-info.java
module com.techoral.user {
    // Required modules
    requires java.base;
    requires java.sql;
    
    // Exported packages
    exports com.techoral.model;
    exports com.techoral.service;
    
    // Services provided
    provides com.techoral.service.UserService
        with com.techoral.service.impl.UserServiceImpl;
}

Key characteristics of modules:

  • Explicit dependency declaration
  • Strong encapsulation
  • Service provider mechanism
  • Improved security
  • Better performance
  • Reduced memory footprint
Pro Tip

Use modules when you need strong encapsulation and explicit dependency management, especially in large applications. Use packages for organizing code within modules.

3. Key Differences

The following table summarizes the main differences between packages and modules:

Feature Package Module
Introduction Java 1.0 Java 9
Purpose Code organization Strong encapsulation
Dependencies Implicit Explicit
Access Control Access modifiers Module system
Descriptor None module-info.java
Services No built-in support Service provider mechanism

4. Module System

The Java Platform Module System (JPMS) provides several benefits over traditional package-based organization.

4.1 Module Descriptor

A module descriptor (module-info.java) defines a module's characteristics:

module com.techoral.app {
    // Dependencies
    requires java.base;
    requires java.sql;
    requires com.techoral.utils;
    
    // Exported packages
    exports com.techoral.model;
    exports com.techoral.service;
    
    // Open packages for reflection
    opens com.techoral.internal;
    
    // Services
    provides com.techoral.service.UserService
        with com.techoral.service.impl.UserServiceImpl;
    uses com.techoral.service.NotificationService;
}

4.2 Module Types

  • Named Modules: Explicitly defined modules with module-info.java
  • Automatic Modules: JAR files without module-info.java
  • Unnamed Module: Classpath-based code
// Example of a multi-module project structure
project/
├── app/
│   ├── src/
│   │   └── main/
│   │       └── java/
│   │           ├── module-info.java
│   │           └── com/
│   │               └── techoral/
│   │                   └── app/
│   └── build.gradle
├── utils/
│   ├── src/
│   │   └── main/
│   │       └── java/
│   │           ├── module-info.java
│   │           └── com/
│   │               └── techoral/
│   │                   └── utils/
│   └── build.gradle
└── settings.gradle

5. Code Examples

Let's look at practical examples of packages and modules.

5.1 Package Organization

// Package structure
package com.techoral.model;

public class User {
    private String name;
    private String email;
    
    public User(String name, String email) {
        this.name = name;
        this.email = email;
    }
    
    // Getters and setters
}

package com.techoral.service;

import com.techoral.model.User;

public interface UserService {
    User createUser(String name, String email);
    User findUser(String email);
    void deleteUser(String email);
}

5.2 Module Organization

// Module descriptor
module com.techoral.user {
    requires java.base;
    requires java.sql;
    requires com.techoral.utils;
    
    exports com.techoral.model;
    exports com.techoral.service;
    
    provides com.techoral.service.UserService
        with com.techoral.service.impl.UserServiceImpl;
}

// Service implementation
package com.techoral.service.impl;

import com.techoral.service.UserService;
import com.techoral.model.User;

public class UserServiceImpl implements UserService {
    @Override
    public User createUser(String name, String email) {
        return new User(name, email);
    }
    
    // Other method implementations
}

6. Best Practices

Follow these best practices when working with packages and modules:

6.1 Package Organization

  • Use meaningful package names
  • Follow the reverse domain name convention
  • Group related classes together
  • Use appropriate access modifiers

6.2 Module Design

  • Keep modules focused and cohesive
  • Minimize module dependencies
  • Use services for loose coupling
  • Consider migration path for existing code
Best Practice

Start with packages for small projects and consider modules when your application grows in size and complexity, or when you need stronger encapsulation.

7. Conclusion

Understanding the differences between packages and modules is essential for modern Java development.

Key takeaways:

  • Packages provide basic code organization
  • Modules offer strong encapsulation and dependency management
  • Modules were introduced in Java 9
  • Both can be used together effectively
  • Choose the right approach based on project needs
About Techoral

Techoral is your go-to resource for Java development, Spring Boot, and test automation. We provide comprehensive guides, tutorials, and best practices for developers.

Popular Topics
Java Packages Java Modules Java 9 Best Practices
Subscribe to Our Newsletter

Get the latest updates and exclusive content delivered to your inbox!