Java Packages
A package in Java is a way to group related classes, interfaces, and sub-packages together. Think of a package like a folder on a computer — it organizes files logically, prevents naming conflicts, and controls access between different parts of a program.
Why Use Packages?
- Organization: Group related classes together for better project structure.
- Namespace management: Two classes can have the same name if they belong to different packages.
- Access control: Packages restrict visibility using access modifiers.
- Reusability: Packages can be reused across multiple projects.
Types of Packages
1. Built-in Packages (Java API)
Java comes with a rich library of pre-built packages. These are part of the Java Standard Library (Java API).
| Package | Description |
|---|---|
| java.lang | Core classes like String, Math, Object, System (auto-imported) |
| java.util | Utility classes: Scanner, ArrayList, HashMap, Arrays |
| java.io | Input/output: File, FileReader, BufferedReader |
| java.math | BigInteger, BigDecimal for precise arithmetic |
| java.net | Networking: URL, Socket, HttpURLConnection |
| java.sql | Database connectivity: Connection, Statement, ResultSet |
| java.time | Date and time: LocalDate, LocalTime, DateTimeFormatter |
2. User-Defined Packages
Developers create their own packages to organize their application classes.
Creating a User-Defined Package
The package statement must be the first line in the Java source file (before any import or class declaration).
// File: com/myapp/utils/MathUtils.java
package com.myapp.utils;
public class MathUtils {
public static int add(int a, int b) {
return a + b;
}
public static double power(double base, int exp) {
double result = 1;
for (int i = 0; i < exp; i++) {
result *= base;
}
return result;
}
}The package name com.myapp.utils maps to the folder structure com/myapp/utils/ on disk.
Importing a Package
To use a class from another package, it must be imported using the import statement.
Import a Specific Class
import com.myapp.utils.MathUtils;
public class Main {
public static void main(String[] args) {
System.out.println(MathUtils.add(10, 20));
System.out.println(MathUtils.power(2, 8));
}
}Import All Classes from a Package (Wildcard)
import com.myapp.utils.*; // imports all classes from the packageThis is convenient but considered less clear than importing specific classes — it makes it harder to see which classes are being used.
Built-in Package Example – java.util
import java.util.ArrayList;
import java.util.Collections;
public class SortDemo {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(5);
numbers.add(2);
numbers.add(8);
numbers.add(1);
Collections.sort(numbers);
System.out.println("Sorted: " + numbers); // [1, 2, 5, 8]
}
}The java.lang Package
java.lang is the only package that is automatically imported in every Java program. It contains fundamental classes like:
StringMathObjectSystemInteger,Double, and other wrapper classes
// No import needed for java.lang
double result = Math.sqrt(144);
System.out.println(result); // 12.0Packages and Access Modifiers
Packages interact closely with access modifiers:
- public class/member: Accessible from any package.
- default (no modifier) class/member: Accessible only within the same package.
- protected member: Accessible in the same package and subclasses in other packages.
- private member: Accessible only within the same class.
Fully Qualified Class Name
Instead of importing, a class can be referred to by its fully qualified name — the complete path including the package name. This is useful when two packages have a class with the same name.
// Using fully qualified name instead of import
java.util.Date today = new java.util.Date();
System.out.println("Today: " + today);Package Naming Convention
Package names follow a standard naming convention to ensure uniqueness:
- All lowercase letters.
- Reverse domain name of the organization, followed by the project structure.
- Example:
com.google.search,org.apache.http,com.company.project.module
Summary
- A package groups related classes and interfaces to organize code and prevent name conflicts.
- Java has many built-in packages:
java.lang,java.util,java.io, and more. - User-defined packages are created using the
packagekeyword as the first line of a file. - Classes from other packages are accessed using the
importstatement. java.langis automatically imported in every Java program.- Package names should be all lowercase and follow the reverse domain convention.
