• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

The Problem

As projects grow, name collisions become inevitable — two developers may independently create a Customer class. Without a namespace system, large applications become unmanageable, with naming conflicts and no logical organization of related classes.

Core Idea

A package is a named namespace that organizes related classes and interfaces. The package declaration at the top of a file specifies which package the class belongs to. The import statement allows referencing classes from other packages without fully qualifying their names.

How It Works

Packages map to directory structure: com.example.myapp corresponds to com/example/myapp/. The fully qualified class name (e.g., java.util.ArrayList) uniquely identifies every class. The compiler and JVM use the package name to locate .class files. Package-private (default) access restricts visibility to classes within the same package.

Visual Explanation

java_packages App com.example.myapp Model model User.java Product.java App->Model Service service UserService.java App->Service Util util Helper.java App->Util Service->Model imports JavaUtil java.util (external) ArrayList, HashMap Service->JavaUtil imports

Semantic Network

semantic_packages THIS Packages ACCESS Access Modifiers THIS--ACCESS built from OOP OOP in Java THIS--OOP built from ENCAP Encapsulation THIS--ENCAP builds into COLL Collections THIS--COLL related

Key Properties

  • Naming convention: Reversed domain name (com.example.project), all lowercase
  • Directory structure: Must match package hierarchy
  • import: Two forms — explicit (import java.util.ArrayList) and wildcard (import java.util.*)
  • Static import: import static allows accessing static members without class name

Connections

Edge Cases & Gotchas

  • Default package: Classes without a package declaration are in the unnamed (default) package — cannot be imported by classes in named packages
  • Import ordering: No functional impact — pure style convention
  • Package and module: Java 9+ modules add another layer of encapsulation above packages
  • Wildcard import does not import subpackages: import java.* does not import java.util.*