Modules to Packages
Modules to Packages
Introduction
In this lesson, you'll learn about modules to packages in Java. Coming from Python, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In Python, you're familiar with modules to packages.
Java has its own approach to modules to packages, which we'll explore step by step.
The Java Way
Let's see how Java handles this concept. Here's a typical example:
// Package declaration (must match directory structure)
package com.example.myapp;
// Standard library imports
import java.lang.Math; // auto-imported — not needed
import java.nio.file.Path;
import java.nio.file.Files;
public class Main {
public static void main(String[] args) {
System.out.println(Math.sqrt(16));
System.out.println(Files.exists(Path.of("file.txt")));
// No __name__ check — main() IS the entry point
}
}
// Third-party: declare in pom.xml (Maven) or build.gradle
// <dependency>
// <groupId>com.squareup.okhttp3</groupId>
// <artifactId>okhttp</artifactId>
// </dependency>Comparing to Python
Here's how you might have written similar code in Python:
# Using standard library modules
import math
from os import path
from pathlib import Path
print(math.sqrt(16))
print(path.exists("file.txt"))
# Entry point guard
if __name__ == "__main__":
print("Running directly")
# Third-party (pip install requests)
# import requestsYou may be used to different syntax or behavior.
Python modules are single files; Java packages are directories matching a com.company.app hierarchy
You may be used to different syntax or behavior.
Java package com.example is declared at the top of every file in that package
You may be used to different syntax or behavior.
Java has no __name__ guard — main() is always the entry point
You may be used to different syntax or behavior.
java.lang (Math, String, System) is auto-imported; everything else needs an explicit import
You may be used to different syntax or behavior.
pip install → Maven (pom.xml) or Gradle (build.gradle) dependency declarations
Step-by-Step Breakdown
1. Package Declaration
Every Java file in a package starts with a package statement. The package name must match the directory path.
# Python: the file location IS its module path
# myapp/utils.py → from myapp.utils import ...// src/main/java/com/example/utils/Helper.java
package com.example.utils;
public class Helper { }2. Importing Classes
Java imports are per class, not per file. import java.util.* imports all classes in a package.
from os import path
import jsonimport java.nio.file.Path;
import java.util.*; // all of java.util3. No __name__ in Java
Java has no concept of __name__. The JVM always starts at the class specified on the command line and calls its main() method.
if __name__ == "__main__":
main()// Just define main() — it is always the entry point
public static void main(String[] args) {
// ...
}4. Dependency Management
pip install is replaced by declaring dependencies in pom.xml (Maven) or build.gradle (Gradle). They download from Maven Central.
# requirements.txt
requests==2.31.0
# pip install -r requirements.txt<!-- pom.xml -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.12.0</version>
</dependency>Common Mistakes
When coming from Python, developers often make these mistakes:
- Python modules are single files; Java packages are directories matching a com.company.app hierarchy
- Java package com.example is declared at the top of every file in that package
- Java has no __name__ guard — main() is always the entry point
Key Takeaways
- Python files = modules; Java directories = packages
- package com.example declared at top of every file
- pip → Maven/Gradle; PyPI → Maven Central