Java Packages

 

As your Java project grows, managing hundreds of classes in a single place becomes messy. This is where Java Packages come in.

A package is a namespace that organizes classes and interfaces into a structured manner. They help improve code readability, reusability, and maintainability.

In this guide, we’ll cover:

  • What packages are
  • Types of packages
  • How to create and use packages
  • Importing classes from packages
  • Best practices

What Is a Package in Java?

 

A package in Java is a group of related classes, interfaces, and sub-packages.
Think of it as a folder in your computer where related files are kept together.

Syntax:

package packageName;

Types of Packages

 
  1. Built-in Packages (Java API)
    • Already available in Java.
    • Examples:
      • java.util (Collections, Scanner, Date)
      • java.io (File handling)
      • java.sql (Database connectivity)
  2. User-defined Packages
    • Created by developers to group related classes.

Example: Creating a User-Defined Package

 

Step 1: Create a package

// File: src/com/example/util/Utils.java
package com.example.util;

public class Utils {
  public static String greet(String name) {
    return “Hello, ” + name + “!”;
  }
}

Step 2: Use the package
// File: src/com/example/main/MainApp.java
package com.example.main;

import com.example.util.Utils; // import package

public class MainApp {
  public static void main(String[] args) {
    String msg = Utils.greet(“Alice”);
    System.out.println(msg);
  }
}

Output:

Hello, Alice!

Importing Packages

 

There are multiple ways to import packages:

  1. Import a single class
    import java.util.Scanner;
  2. Import all classes in a package
    import java.util.*;
  3. Fully qualified name (no import)

public class Test {
  public static void main(String[] args) {
    java.util.Scanner sc = new java.util.Scanner(System.in);
  }
}

Sub-Packages

 
  • A package can contain sub-packages.
  • Example:
    • java.util (main package)
    • java.util.regex (sub-package)

Access Modifiers and Packages

 
  • public → accessible from any package
  • protected → accessible within same package + subclasses in other packages
  • default (no modifier) → accessible only within the same package
  • private → accessible only inside the class

Best Practices

 
  • Use packages to group related classes (e.g., model, service, controller).
  • Follow naming conventions: lowercase, reverse domain name (com.company.project).
  • Avoid clutter—don’t put all classes in one package.
  • Use sub-packages for large projects.
  • Keep package structure aligned with project modules.

Conclusion

 

Java packages are essential for organizing code, avoiding naming conflicts, and controlling access.

  • Use built-in packages (like java.util) for common utilities.
  • Create user-defined packages to structure your applications.
  • Import packages effectively for clean and readable code.

By mastering packages, you can build scalable and maintainable Java projects.

Scroll to Top