Packages and Directory Structure
Introduction
Packages group related classes, avoid name clashes, and control visibility. Real Java projects mirror package names in folder paths (com.example.app). This chapter covers package, import, directory layout, and access modifiers—needed before multi-file examples in Interfaces and Collections.
Prerequisites
- Classes
- One Java project in IntelliJ
1) package Declaration
First line in a .java file (before imports):
package com.hellocode.learn;
public class Greeter {
public String hello(String name) {
return "Hello, " + name;
}
}Naming convention: lowercase, reverse domain style:
com.company.project.module
File path must match:
src/com/hellocode/learn/Greeter.javaWarning
package statement must match folder structure. Mismatch causes compile errors in strict builds.
2) import Statements
Import types you use without fully qualified names:
package com.hellocode.learn;
import java.util.List;
import java.util.ArrayList;
public class Demo {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
}
}| Form | Meaning |
|---|---|
import java.util.List; | single type |
import java.util.*; | all types in package (avoid in production style) |
| no import | use full name java.util.List |
java.lang (String, System, Object) is imported automatically.
Static import (occasional):
import static java.lang.Math.max;3) Same Package vs Other Packages
Classes in the same package see package-private (default) members. Other packages need public or inheritance rules for protected.
4) Access Modifiers
| Modifier | Same class | Same package | Subclass (other pkg) | Anywhere |
|---|---|---|---|---|
private | yes | no | no | no |
| default (no keyword) | yes | yes | no | no |
protected | yes | yes | yes | no |
public | yes | yes | yes | yes |
public class Account {
private double balance; // only this class
String owner; // package-private
protected int accountType; // package + subclasses
public void deposit(double amt) { // everyone
}
}Encapsulation guideline: fields private, expose behavior via public methods.
5) IntelliJ / Maven Layout (Preview)
my-app/
src/main/java/com/hellocode/learn/Greeter.java
src/test/java/...Build tools (Maven, Gradle) compile everything under src/main/java using package paths.
6) Module Path (Awareness)
Java 9+ modules (module-info.java) add another layer for large systems. Beginners focus on packages first; modules appear in advanced projects.
Common Beginner Mistakes
Wrong Folder for Package
package com.app; in src/App.java without com/app/ folders fails.
Circular Dependencies
Package A imports B while B imports A—refactor shared code to a third package.
Everything public
Expose only what other packages need.
Mini Practice
Create packages com.hellocode.model and com.hellocode.app; put Student in model, Main in app importing Student.
What’s Next
Interfaces for behavior contracts across packages.
FAQ
Must package name match domain I own?
Convention uses domains you control to avoid global collisions. Learning projects can use com.hellocode.learn.
What is a fully qualified name?
java.util.List—package + class. Used when avoiding import conflicts (java.util.Date vs java.sql.Date).
Can I have multiple classes per package?
Yes. Many classes per package; one public class per file matching filename.
Default package?
Files with no package line are in unnamed default package—fine for tiny demos, avoid in real projects.
How do JAR files relate?
A JAR packages compiled .class files preserving package directories for distribution.