Back to Java
2025-12-135 min read

package

Learn package step by step with clear examples and exercises.

Title: Mastering Java Packages: A full guide

Why This Matters

Java packages serve as a fundamental mechanism for organizing your code effectively, making it easier to manage and reuse. Understanding how they work is crucial for writing efficient and scalable Java applications. This knowledge will also help you in interviews, as many questions revolve around package structures.

Prerequisites

Before diving into packages, make sure you have a solid understanding of the following concepts:

  • Basic Java syntax (variables, methods, classes)
  • Importing classes and libraries
  • Understanding the JDK directory structure
  • Familiarity with access modifiers (public, private, protected, default)

Core Concept

A Java package is a namespace used to organize related classes and interfaces. Packages provide a way to avoid naming conflicts between different sets of classes and promote code reuse by making it easier to locate specific types within an application.

To create a package, simply enclose the source files (.java) and their accompanying resources in a directory that shares the same name as the desired package. For example:

myapp/
src/
main/
java/
com/
example/
Main.java
Utils.java
Resources/
config.properties

In this example, the Main, Utils, and associated resources belong to the com.example package. To use these classes in another part of your application, you'll need to import them:

import com.example.Main;
import com.example.Utils;

Package Naming Conventions

Java packages follow a simple naming convention based on reverse domain notation (e.g., com.example). This helps avoid conflicts with other packages and makes it easier to identify the source of a package.

Access Modifiers and Packages

When you declare classes within a package, you can use the access modifier package-private instead of public. Package-private classes are only visible to other classes within the same package. This helps keep your code organized and prevents unintended interactions between different parts of your application.

Importing Packages

To import an entire package, you can use the wildcard character (*) in your import statement:

import com.example.*;

This will make all public classes and interfaces within the com.example package available for use in your current file.

Package-Private vs Public Access Modifiers

Package-private classes are only visible to other classes within the same package, while public classes can be accessed from any part of the application. It's essential to understand when to use each access modifier to ensure proper encapsulation and organization of your code.

Worked Example

Let's create a simple application with two classes, Main and Utils, that belong to the same package (com.example). The Main class should print "Hello, World!" and call a method from the Utils class that returns the sum of two numbers.

Main.java

package com.example;

public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
double result = Utils.addNumbers(2.5, 3.7);
System.out.printf("The sum is: %.2f%n", result);
}
}

Utils.java

package com.example;

public class Utils {
public static double addNumbers(double a, double b) {
return a + b;
}
}

Common Mistakes

  1. ### Forgetting to Create the Package Directory

Remember to create the package directory structure before placing your source files inside it.

  1. ### Incorrect Import Statements

Ensure you're importing the correct package and using the correct syntax (e.g., import com.example.* instead of import *com.example*).

  1. ### Misusing Access Modifiers

Avoid making classes public if they are intended to be package-private, as this can lead to unintended interactions with other parts of your application.

  1. ### Failing to Organize Code Properly

Proper organization of code within packages helps maintain a clean and scalable application structure. Avoid placing unrelated classes in the same package and consider creating sub-packages when necessary.

Practice Questions

  1. Create a simple Java application with two classes, Main and Utils, that belong to the same package (com.myapp). The Main class should print "Hello, World!" and call a method from the Utils class that returns the sum of two numbers.
  2. Write a Java application that uses multiple packages (e.g., com.myapp, com.utils) with at least three classes in total. The application should demonstrate how to import entire packages and individual classes.
  3. Create a package structure for a simple address book application, including the necessary directories and files to organize your code effectively.
  4. Explain the differences between public, private, protected, and default access modifiers in Java, and provide examples of when each should be used.

FAQ

  1. Why use packages in Java?
  • Organize related classes and interfaces
  • Prevent naming conflicts between different sets of classes
  • Promote code reuse by making it easier to locate specific types within an application
  1. What is the reverse domain notation used for package names in Java?
  • The reverse domain notation helps avoid conflicts with other packages and makes it easier to identify the source of a package. For example, com.example corresponds to the domain name example.com reversed.
  1. What is the difference between public and package-private classes in Java?
  • Public classes are visible to all other classes regardless of their packages, while package-private classes are only visible to other classes within the same package.
  1. Why should I organize my code properly using packages in Java?
  • Proper organization helps maintain a clean and scalable application structure, making it easier for others to understand your code and reducing the likelihood of errors or conflicts.
  1. What is the purpose of access modifiers in Java?
  • Access modifiers control the visibility and accessibility of classes, methods, and variables within an application, ensuring proper encapsulation and organization of your code.
package | Java | XQA Learn