Skip to content

Java Packages

As project sizes grow, the number of classes increases. To better manage these classes, avoid naming conflicts, and control code access permissions, Java provides the Package mechanism. Packages are essentially namespaces used to organize related classes and interfaces.

What is a Package?

You can think of packages as folders in a computer file system. Just as you store documents, images, and videos in different folders, Java also organizes functionally related classes in the same package.

For example, all I/O-related core classes in the Java platform are in the java.io package, and all collection classes are in the java.util package.

Why Use Packages?

  1. Prevent naming conflicts: Two different developers might create classes with the same name, such as Logger. If these two Logger classes are in different packages (like com.companyA.Logger and org.companyB.Logger), they won't conflict.
  2. Access control: Packages provide a level of access protection. If a class or member is not declared as public, protected, or private, it has "package-private" (default) access, meaning it can only be accessed by other classes in the same package.
  3. Organization and location: Grouping related classes together makes code structure clearer and easier to find and use.

Package Naming Conventions

To ensure package name uniqueness, the industry standard is to use a reversed internet domain name as the prefix for package names. Package names use all lowercase letters.

  • For example, if your company's domain is mycompany.com, your package names should start with com.mycompany, followed by project and module names, like com.mycompany.myproject.model.

Creating and Using Packages

Declaring Packages

To place a class in a package, you must declare the package name using the package keyword on the first line of the source file.

java
// File: com/mycompany/utils/StringUtils.java
package com.mycompany.utils;

public class StringUtils {
    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }
}

Important: The source file's directory structure must exactly match the package name. The StringUtils.java file above must be stored in the .../com/mycompany/utils/ directory.

The import Keyword

To use a class from another package, you need to import it into the current file using the import keyword. import statements are placed after the package statement and before class definitions.

  1. Import a single class: This is the most recommended approach as it clearly indicates which class you're using.

    java
    import java.util.ArrayList;
    
    public class MyClass {
        ArrayList<String> list = new ArrayList<>();
    }
  2. Import an entire package (wildcard): Using the asterisk * imports all public classes and interfaces in a package.

    java
    import java.util.*;
    
    public class MyClass {
        List<String> list = new ArrayList<>();
        Map<String, Integer> map = new HashMap<>();
    }
  3. Use fully qualified names: If you don't use import, you can directly use the class's fully qualified name (package name + class name) in your code.

    java
    public class MyClass {
        java.util.ArrayList<String> list = new java.util.ArrayList<>();
    }

Static Import (static import)

Starting from Java 5, import static allows you to import a class's static members (fields or methods), so you can use them directly in your code without going through the class name.

java
import static java.lang.Math.PI;
import static java.lang.Math.sqrt;

public class Circle {
    double radius;

    public double getArea() {
        // No need to write Math.PI
        return PI * radius * radius;
    }

    public double getHypotenuse(double a, double b) {
        // No need to write Math.sqrt
        return sqrt(a*a + b*b);
    }
}

Note: Overusing static imports can reduce code readability because it obscures where static members come from. It's recommended to use it only when you frequently use a small number of static members from a particular class.

The java.lang Package

java.lang is a special package that contains core classes of the Java language, such as Object, String, System, Integer, Math, etc. This package is automatically imported into every Java source file, so you can use these classes directly without manual import.

Content is for learning and research only.