What you'll learn
A real project is dozens or hundreds of classes. Left in one pile they become chaos. This module is about keeping code organised, controlling what's visible, and packaging your app so others can run it.
By the end you'll be able to:
- Group related classes into packages
- Pull in other classes with
importandstatic import - Apply the four access levels with confidence
- Understand the classpath and build runnable JAR files
- Get a first look at the Java module system
Packages
A package is a namespace that groups related classes — like folders for your code. It prevents name clashes (two Utils classes can coexist in different packages) and mirrors your folder structure on disk. You declare it as the first line of a file, using reverse-domain naming:
package com.mastercoding.shapes; // must be the first line
public class Circle {
public double area(double r) {
return Math.PI * r * r;
}
}Package com.mastercoding.shapes maps to the folder com/mastercoding/shapes/.
Note
com.yourcompany.project) so package names are globally unique. Package names are always lowercase.Imports & static imports
To use a class from another package, you import it (or write its fully-qualified name every time). A static import goes further and pulls in a class's static members directly, so you can call sqrt() instead of Math.sqrt():
import com.mastercoding.shapes.Circle; // bring the class in
public class Main {
public static void main(String[] args) {
Circle c = new Circle();
System.out.printf("%.2f%n", c.area(2));
}
}import static java.lang.Math.*; // import the members, not the class
public class Main {
public static void main(String[] args) {
double d = sqrt(pow(3, 2) + pow(4, 2)); // no "Math." prefix
System.out.println(d);
}
}Tip
java.lang (like String, Math, System) are imported automatically — that's why you've never had to import them. Avoid wildcard imports (import java.util.*) in large projects; explicit imports read more clearly.Access-control rules
Access modifiers decide who can see a class, field, or method. Choosing the tightest level that still works is a core habit of clean design — it keeps your public surface small and your internals free to change:
| Modifier | Same class | Same package | Subclass | Everywhere |
|---|---|---|---|---|
public | ✓ | ✓ | ✓ | ✓ |
protected | ✓ | ✓ | ✓ | ✗ |
| (default) | ✓ | ✓ | ✗ | ✗ |
private | ✓ | ✗ | ✗ | ✗ |
Tip
private and open up only when you must. It's far easier to loosen access later than to tighten a field half the codebase now depends on.Classpath & JAR files
When you run a program, the JVM finds your classes via the classpath — the list of folders and archives to search. To ship an app you bundle its compiled .class files into a single JAR (Java ARchive, really just a ZIP). Mark a main class and it becomes an executable JAR anyone can run with java -jar:
# compile every source file into an 'out' folder
javac -d out src/com/mastercoding/**/*.java
# bundle the compiled classes into a runnable JAR
jar --create --file app.jar --main-class com.mastercoding.app.Main -C out .
# run it anywhere Java is installed
java -jar app.jarNote
The Java module system
For very large applications, Java 9 introduced modules — a layer above packages. A module-info.java file declares what a module requires (its dependencies) and what packages it exports (its public API). This creates strong encapsulation at the library level and lets you build slim custom runtimes:
module com.mastercoding.app {
requires java.sql; // this module depends on java.sql
exports com.mastercoding.api; // expose this package to others
}Note
jlink in Module 39.Recap & quick check
Key takeaways
- Packages group related classes into namespaces that mirror folders; declared as the first line.
- import brings in classes; static import brings in static members (call sqrt() directly).
- Access levels from open to closed: public, protected, package-private (default), private.
- The classpath tells the JVM where to find classes; a JAR bundles them for distribution.
- Modules (module-info.java) add library-level encapsulation with requires and exports.
Quick check
1. Where must a package declaration appear in a source file?
2. Which access modifier is the most restrictive?
3. What does a static import let you do?
4. What is a JAR file?
5. In module-info.java, what does 'exports' do?
Well done — your projects can now scale cleanly. Next up: Module 12 — Exception Handling, where you'll make programs robust in the face of errors.