Packages & Imports
Real Java programs contain hundreds of classes. Packages group related classes into namespaces — like folders — so names don't clash and code stays organised. Imports let you use classes from other packages, including the huge standard library, by their short names.
Using classes from other packages
import java.time.LocalDate; import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { List<String> tasks = new ArrayList<>(); tasks.add("Write report"); LocalDate due = LocalDate.of(2026, 3, 31); System.out.println(tasks + " due " + due); java.util.Random r = new java.util.Random(7); System.out.println(r.nextInt(100) >= 0); } }
[Write report] due 2026-03-31 true
An import lets you write List instead of the fully qualified name java.util.List. You can always use the full name instead, as with java.util.Random above. Classes in java.lang — String, Math, System, Integer — are imported automatically.
Import forms
| Form | Imports |
|---|---|
import java.util.List; | One class |
import java.util.*; | Every class in java.util (not sub-packages) |
import static java.lang.Math.PI; | A static member, used as just PI |
import module java.base; | Every package exported by a module (Java 25) |
Single-class imports make dependencies explicit and are what IDEs generate. Compact source files automatically import the whole java.base module, so List, Map and friends work without imports there — but classic class files need them.
import static java.lang.Math.PI; import static java.lang.Math.sqrt; void main() { System.out.println(PI * 2); System.out.println(sqrt(81)); }
6.283185307179586 9.0
Declaring a package
package com.example.shop.model; public class Product { private final String name; private final double price; public Product(String name, double price) { this.name = name; this.price = price; } public String name() { return name; } public double price() { return price; } }
The package statement comes first in the file. By convention, package names are lowercase and start with a reversed domain you control (com.example), so they're globally unique. The folder structure mirrors the package:
src/main/java/
└── com/example/shop/
├── App.java package com.example.shop;
├── model/
│ ├── Product.java package com.example.shop.model;
│ └── Order.java
└── service/
└── OrderService.java package com.example.shop.service;
package com.example.shop; import com.example.shop.model.Product; public class App { public static void main(String[] args) { Product p = new Product("Desk Lamp", 34.00); System.out.println(p.name() + " costs " + p.price()); } }
Packages and access
Classes and members without an access modifier are visible only inside their own package. That lets a package keep helper classes internal while exposing a small public API. Each .java file can contain at most one public top-level class, and it must match the file name.
Name clashes
import java.util.List; public class Main { public static void main(String[] args) { List<Integer> numbers = List.of(1, 2, 3); java.awt.Point point = new java.awt.Point(3, 4); System.out.println(numbers + " " + point.x); } }
[1, 2, 3] 3
Two packages can contain classes with the same name — java.util.List and java.awt.List. Import one and use the fully qualified name for the other.
Modules
Since Java 9, packages can be grouped into modules, declared in a module-info.java file that states which packages a module exports and which modules it requires. The JDK itself is split into modules (java.base, java.sql, java.net.http…). Many applications don't declare modules of their own; build tools and the classpath work fine without them.
Try it yourself
Use classes from three different packages in one program.
2026-12-25 is a Friday