TExcelTAD
Home › Java › Java Packages & import

Java Packages & import

As programs grow to hundreds of classes, you need a way to organise them and avoid name clashes. Java uses packages — named groups of related classes, much like folders on a disk. The standard library alone has hundreds of packages, such as java.util (collections), java.time (dates) and java.nio.file (files).

Using classes from other packages

A class's fully qualified name includes its package: java.util.ArrayList, java.time.LocalDate. You can always write that full name, but it's long:

java · try it yourself
class Main {
    public static void main(String[] args) {
        java.util.List<String> names = new java.util.ArrayList<>();
        names.add("Ada");
        names.add("Grace");
        System.out.println(names);
    }
}
[Ada, Grace]

An import statement at the top of the file lets you use the short name instead:

java · try it yourself
import java.util.ArrayList;
import java.util.List;
import java.time.LocalDate;

class Main {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>();
        names.add("Ada");
        names.add("Grace");
        System.out.println(names);

        LocalDate launch = LocalDate.of(1995, 5, 23);
        System.out.println("Java was announced on " + launch);
        System.out.println("That was a " + launch.getDayOfWeek());
    }
}
[Ada, Grace]
Java was announced on 1995-05-23
That was a TUESDAY

Kinds of import

Form Imports
import java.util.Scanner; one class (single-type import)
import java.util.*; every class of java.util (on demand) — not its sub-packages
import static java.lang.Math.sqrt; a static member, used without the class name
import static java.lang.Math.*; all static members of Math
import module java.base; every package a module exports (Java 25+)

Classes in java.lang — String, Math, System, Integer and others — are imported automatically, which is why you never had to import them.

java · try it yourself
import java.util.*;
import static java.lang.Math.*;

class Main {
    public static void main(String[] args) {
        Map<String, Integer> stock = new TreeMap<>();
        stock.put("pears", 4);
        stock.put("apples", 9);
        System.out.println(stock);
        System.out.println(sqrt(49) + max(3, 8) + PI);
    }
}
{apples=9, pears=4}
18.141592653589793

Tip: Single-type imports make it obvious where each class comes from; most teams and IDEs prefer them over *. Static imports are best kept for a few heavily used members, like constants or test assertions.

Name clashes

If two packages have a class with the same name — java.util.Date and java.sql.Date — you can import only one of them by its short name. Refer to the other by its fully qualified name:

java
import java.util.Date;

Date now = new Date();                       // java.util.Date
java.sql.Date sqlDate = java.sql.Date.valueOf("2026-01-31");

Creating your own packages

To put a class in a package, write a package declaration as the first line of the file, and store the file in a matching folder structure:

src/
└── com/
    └── example/
        ├── shop/
        │   ├── Product.java      // package com.example.shop;
        │   └── Cart.java         // package com.example.shop;
        └── app/
            └── Main.java         // package com.example.app;
java
// File: src/com/example/shop/Product.java
package com.example.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 getName() { return name; }
    public double getPrice() { return price; }
}
java
// File: src/com/example/app/Main.java
package com.example.app;

import com.example.shop.Product;

public class Main {
    public static void main(String[] args) {
        Product p = new Product("Mug", 8.5);
        System.out.println(p.getName() + ": " + p.getPrice());
    }
}

Compile and run from the src folder:

bash
javac com/example/shop/Product.java com/example/app/Main.java
java com.example.app.Main
Mug: 8.5

Note: Playgrounds run a single file without a folder structure, so examples on ExcelTAD don't use a package line. In real projects every class belongs to a package — build tools like Maven and Gradle set up the folders for you.

Naming conventions

  • Package names are all lowercase.
  • Use your organisation's domain reversed as a prefix to make names globally unique: com.example, org.apache.commons, io.github.yourname.
  • Then add the project and module: com.example.shop.billing.

Packages also affect visibility: members without an access modifier (package-private) are visible only to classes in the same package. This is how packages help with encapsulation at a larger scale.

Test yourself

Which package is imported automatically into every Java file?

  1. java.util
  2. java.io
  3. java.lang
  4. java.base

Does import java.util.*; also import java.util.function.Function?

  1. Yes, sub-packages are included
  2. No, sub-packages need their own import
  3. Only in Java 25+
  4. Only for static members

Where must the package declaration appear?

  1. Anywhere in the file
  2. After the imports
  3. As the first statement in the file
  4. Inside the class