9.1 Why Packages?
A package is a namespace that groups related types, preventing name clashes (two libraries can each have a Logger), enabling access control (package-private visibility), and mapping onto the directory structure. Convention: reverse domain name — com.university.exams.
package com.university.exams; // MUST be the first non-comment statement
public class ResultCalculator { /* ... */ }
Directory rule: the file must live at com/university/exams/ResultCalculator.java relative to a classpath root. Compile with javac -d out src/com/university/exams/ResultCalculator.java (the -d flag creates the package directories under out), then run with the fully qualified name: java -cp out com.university.exams.ResultCalculator.
9.2 import — Sugar, Not Loading
import merely lets you omit the package prefix; it copies nothing and loads nothing at runtime.
import java.util.List;— single type (preferred).import java.util.;— on-demand; does not include subpackages (java.util.≠java.util.stream.*).import static java.lang.Math.PI;— static import of members: writesqrt(PI)instead ofMath.sqrt(Math.PI).java.langis imported automatically — that's whyString,System,Mathneed no import.- Name clash (
java.util.Datevsjava.sql.Date)? Import one, fully qualify the other.
Classpath: the list of roots (directories/JARs) where the JVM's class loader searches for .class files — set via -cp/-classpath or the CLASSPATH variable; default is the current directory. NoClassDefFoundError/ClassNotFoundException at launch is almost always a classpath mistake.
9.3 The Four Access Modifiers — Master Matrix
| Access level | Same class | Same package | Subclass (other pkg) | World |
|---|---|---|---|---|
private | ✔ | ✘ | ✘ | ✘ |
| default (no keyword) | ✔ | ✔ | ✘ | ✘ |
protected | ✔ | ✔ | ✔ | ✘ |
public | ✔ | ✔ | ✔ | ✔ |
Memorize this matrix — it is asked verbatim, usually for 5 marks.
Fine print that earns the last mark:
- Top-level classes may only be
publicor default — never private/protected. - Only one public top-level class per file, and the filename must match it.
protectedacross packages works only through inheritance — a subclass in another package can access the member on itself/its own type, not on an arbitrary parent instance.- Overriding may widen access (protected → public) but never narrow it (Lesson 6).
9.4 Encapsulation — The Modifiers' Purpose
Encapsulation = private state + controlled access through methods. It buys you validation, read-only or write-only views, and freedom to change the internal representation without breaking callers.
public class Student {
private int marks; // hidden state
public int getMarks() { return marks; } // read access
public void setMarks(int m) { // guarded write access
if (m < 0 || m > 100) throw new IllegalArgumentException("0-100 only");
this.marks = m;
}
}
A class with getters only is immutable-ish (read-only); the fully immutable recipe: final class, private final fields, no setters, defensive copies of mutable fields — exactly how String is built.
9.5 JARs & the Standard Library Layout
A JAR (Java ARchive) is a zipped tree of packages + a manifest: create with jar cfe app.jar com.uni.Main -C out ., run with java -jar app.jar. Know the flagship packages: java.lang (core, auto-imported), java.util (collections), java.io/java.nio (I/O), java.sql (JDBC), javax.swing/java.awt (GUI), java.net (networking).
🎯 Exam Focus
- What is a package? Explain how to create, compile (
javac -d) and run a program in a user-defined package, with the directory structure. - Reproduce the access-modifier matrix for all four levels against: same class, same package, subclass in another package, and the world.
- Explain
protected's special behaviour across packages with a code example that compiles and one that does not. - Differentiate
importandimport static. Doesimport java.util.*importjava.util.stream? Why or why not? - Define encapsulation. Write an immutable class
Accountand list the four ingredients of immutability. - What are CLASSPATH and JAR files? How does the class loader locate
com.uni.Main?