Java Packages and Import
A real program has dozens or hundreds of classes. Java groups related classes into packages, the way you group files into folders. A package also acts as a name space: two classes may have the same name if they are in different packages. You have used packages from the start: Scanner is in java.util, and String and Math are in java.lang.
Using classes from the Java library
To use a class from another package, write an import line at the top of the file, before the class. java.lang is imported automatically, which is why String, Math and System never need it:
import java.util.ArrayList;
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Java");
System.out.println(list);
LocalDate d = LocalDate.of(2025, 1, 26);
System.out.println(d.getDayOfWeek());
}
}[Java] SUNDAY
You can import every class of a package with a star, such as import java.util.*;, but importing each class by name makes clear where every class comes from. You can also skip the import and write the full name, like java.util.Scanner.
Some important packages
| Package | Contains |
|---|---|
java.lang | String, Math, System, Integer… (imported automatically) |
java.util | Scanner, ArrayList, HashMap, Arrays, Random… |
java.time | dates and times: LocalDate, LocalTime, Duration… |
java.nio.file | files and folders: Files, Path |
java.util.stream | streams of data |
Making your own package
A class joins a package with a package line as the very first line of its file, and the file must be in a folder with the same name. Here Product is in the package shop, so it lives in shop/Product.java. Try it in the editor opens both files in their folders:
import shop.Product;
public class Main {
public static void main(String[] args) {
Product p = new Product("Notebook", 45.0);
System.out.println(p.label());
System.out.println(p.priceWithTax());
}
}package shop;
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 label() {
return name + " - " + price;
}
public double priceWithTax() {
return price * 1.05;
}
}Notebook - 45.0 47.25
Because Main is in a different package, it can only use what Product marks public: the class, the constructor and the two methods. This is the "package access" rule from Modifiers at work.
Naming packages
Package names are written in small letters. Companies usually start them with their web address backwards, such as com.example.shop, which becomes the folder path com/example/shop. Each dot is one more folder. Small programs, like the ones in this tutorial, can stay in the default package (no package line), which is what Main does.
Practice exercises
- Write a program that imports
java.util.Randomand prints a random number from 1 to 6. - Create a package
geometrywith a public classCircle(radius, area method) and use it fromMain. - Remove the
publicfrom theCircleconstructor and read the error you get inMain.