Modules to Packages
Modules to Packages
Introduction
In this lesson, you'll learn about modules to packages in Java. Coming from TypeScript, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In TypeScript, 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:
// File: com/example/utils/MathUtils.java
package com.example.utils;
public class MathUtils {
public static final double PI = 3.14159;
public static int add(int a, int b) {
return a + b;
}
}
// File: com/example/Main.java
package com.example;
import com.example.utils.MathUtils;
import com.example.utils.*; // wildcard import
public class Main {
public static void main(String[] args) {
System.out.println(MathUtils.add(2, 3));
}
}Comparing to TypeScript
Here's how you might have written similar code in TypeScript:
// utils/math.ts
export function add(a: number, b: number): number {
return a + b;
}
export const PI = 3.14159;
// main.ts
import { add, PI } from "./utils/math";
import type { User } from "./types";You may be used to different syntax or behavior.
TypeScript uses file-based ES modules; Java uses package declarations
You may be used to different syntax or behavior.
Java package names mirror the directory structure (com.example.utils)
You may be used to different syntax or behavior.
No export keyword — public class/method is importable from outside the package
You may be used to different syntax or behavior.
Static utility methods live in classes (no standalone functions)
Step-by-Step Breakdown
1. package Declaration
Every Java file starts with a package declaration that mirrors its folder path. com.example.utils → src/com/example/utils/.
// TypeScript: folder structure is implicit
export function add() {}package com.example.utils;
public class MathUtils {
public static int add(int a, int b) { return a + b; }
}2. import Statements
Java import uses fully qualified class names. import com.example.utils.MathUtils imports a single class. import com.example.utils.* imports all public classes in that package.
import { add } from "./utils/math";import com.example.utils.MathUtils;
// then use: MathUtils.add(2, 3)3. No Standalone Functions — Use Static Methods
Java has no module-level functions. Utility functions are static methods in utility classes.
export function clamp(n: number, min: number, max: number): number {}public class NumberUtils {
public static int clamp(int n, int min, int max) {
return Math.max(min, Math.min(max, n));
}
}4. Build Tools: Maven and Gradle
npm manages packages in TypeScript. Java uses Maven (pom.xml) or Gradle (build.gradle) as both build tools and package managers, pulling from Maven Central.
// package.json
{
"dependencies": { "axios": "^1.0" }
}<!-- pom.xml -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.14</version>
</dependency>Common Mistakes
When coming from TypeScript, developers often make these mistakes:
- TypeScript uses file-based ES modules; Java uses package declarations
- Java package names mirror the directory structure (com.example.utils)
- No export keyword — public class/method is importable from outside the package
Key Takeaways
- Java uses package declarations that mirror the directory structure
- import uses fully qualified class names (com.example.utils.MyClass)
- No standalone functions — use public static methods in classes
- Maven (pom.xml) or Gradle is the Java equivalent of npm