Shiklo Programming
EN
  • English
  • Español
  • Português
  • Deutsch
  • Français
  • 日本語
  • हिन्दी

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());
    }
}
Output
[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

PackageContains
java.langString, Math, System, Integer… (imported automatically)
java.utilScanner, ArrayList, HashMap, Arrays, Random…
java.timedates and times: LocalDate, LocalTime, Duration…
java.nio.filefiles and folders: Files, Path
java.util.streamstreams 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:

Example (2 files)Try it in the editor
Main.java
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());
    }
}
shop/Product.java
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;
    }
}
Output
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

  1. Write a program that imports java.util.Random and prints a random number from 1 to 6.
  2. Create a package geometry with a public class Circle (radius, area method) and use it from Main.
  3. Remove the public from the Circle constructor and read the error you get in Main.