← Back to Home

Packages in Java

A package in Java is a namespace that groups related classes and interfaces together. Packages help organize large applications, avoid naming conflicts, control access, and improve maintainability. This is a high-frequency interview topic and fundamental to real-world Java project structure.

What Is a Package?

  • A logical container for classes and interfaces
  • Provides a namespace
  • Supports access control
  • Improves code organization and reuse
package com.company.project.module;
          

Why Packages Are Needed

  • Organize large codebases
  • Avoid class name conflicts
  • Control visibility using access modifiers
  • Enable modular development
  • Improve readability and maintenance

Types of Packages in Java

1️⃣ Built-in Packages

Provided by Java standard library.

Examples:

  • java.lang
  • java.util
  • java.io
  • java.sql
  • java.time

✔ Automatically available:

java.lang.*
          

2️⃣ User-Defined Packages

Created by developers to organize application code.

package com.bank.account;
          

Creating a Package

1. Using package Keyword

package com.example.util;

public class MathUtil {
    public static int add(int a, int b) {
        return a + b;
    }
}
          

Rules:

  • package statement must be first line
  • Only one package statement per class

2. Compile with Directory Structure

javac -d . MathUtil.java
          

✔ Creates folder structure automatically

Package Naming Conventions (Very Important)

  • All lowercase
  • Reverse domain name format
com.company.project.module
          

Examples:

  • com.google.common
  • org.apache.commons

Accessing Classes from a Package

1. Using Fully Qualified Name

com.example.util.MathUtil.add(2, 3);
          

2. Using import Statement

import com.example.util.MathUtil;

MathUtil.add(2, 3);
          

3. Import All Classes (Wildcard)

import com.example.util.*;
          

⚠️ Imports only classes, not sub-packages

Access Modifiers and Packages (Interview Favorite)

Modifier Same Class Same Package Subclass (diff pkg) Everywhere
private
default
protected
public

✔ Packages define visibility boundaries

Sub-Packages

com.company
 └── util
 └── service
          
  • ✔ Sub-packages are independent
  • ✔ No automatic access between parent and child packages

Static Import (Java 5+)

Allows direct access to static members.

import static java.lang.Math.*;

double r = sqrt(16);
          
  • ✔ Improves readability
  • ⚠️ Use sparingly

Package vs Folder (Clarification)

Aspect Package Folder
Concept Logical grouping Physical directory
Java-level Yes No
Naming Namespace File system
Relation Often mapped Not mandatory

JAR and Packages

  • Packages are bundled into JAR files
  • JARs are used for distribution and reuse
jar cf utils.jar com/example/util/*.class
          

Common Beginner Mistakes

  • Forgetting package statement
  • Incorrect folder structure
  • Using uppercase letters in package names
  • Assuming sub-packages inherit access
  • Overusing wildcard imports

Interview-Ready Answers

Short Answer

A package is a namespace that groups related classes and interfaces in Java.

Detailed Answer

In Java, packages are used to organize classes and interfaces into logical units, prevent naming conflicts, and control access using access modifiers. Java provides built-in packages, and developers can create custom packages using the package keyword.

Key Takeaway

Packages bring structure, safety, and scalability to Java applications. They are essential for clean architecture, modular development, and enterprise-level codebases.