Java Packages

Last Updated : 24 Nov, 2026

A package in Java is a namespace used to group related classes, interfaces, enums, and other types. It helps prevent naming conflicts and provides a structured way to manage application components.

  • Java provides built-in packages and allows programmers to create their own packages.
  • Packages support access control and reuse of related code.Types of Java Packages

Types of Packages

Java packages are mainly classified into two types:

frame_1000002571
Types of Packages in Java

1. Built-in Packages

Built-in packages are provided by the Java API. They contain predefined classes and interfaces for common programming tasks.

  • java.lang contains core classes such as String, Math, and System.
  • java.io provides classes for input/output operations and file handling.
  • java.util provides utility classes such as ArrayList, HashMap, and Random.
  • java.awt provides components for graphical user interfaces.

Note: The java.lang package is automatically imported into every Java source file.

Java
import java.util.Random;
public class Geeks{
    public static void main(String[] args){

        // Create Random object
        Random rand = new Random();

        // Generate a number from 0 to 99
        int number = rand.nextInt(100);
        System.out.println("Random number: " + number);
    }
}

Output
Random number: 59

Explanation: The program imports the Random class and uses nextInt(100) to generate a random integer between 0 and 99. The output may vary with each execution.

2. User-defined Packages

User-defined packages are created by programmers to organize application-specific classes and interfaces.

Example: Java program demonstrating the creation and use of a user-defined package.

Step 1: Create Helper.java

Create a file named Helper.java inside the com/myapp directory.

package com.myapp;
public class Helper{
public static void show(){
System.out.println("Hello from Helper!");
}
}

Step 2: Create Test.java

Create Test.java in the project root directory.

import com.myapp.Helper;
public class Test{
public static void main(String[] args){
// Call method from another package
Helper.show();
}
}

Step 3: Compile and Run

Open the terminal in the project root directory and execute:

javac -d out com/myapp/Helper.java Test.java
java -cp out Test

Output

Hello from Helper!

Explanation: The Helper class belongs to the com.myapp package. The Test class imports it and calls its public static show() method.

Package Declaration and Folder Structure

The package statement specifies the package to which a Java type belongs. It must appear before import statements and type declarations, except for comments and blank lines.

Syntax:

package packageName;

The directory structure conventionally follows the package name, with dots separating directory levels.

For the previous example, the source files are arranged as follows:

Screenshot-2026-11-21-115541
Folder Structure of a User-Defined Package in Java

Accessing Classes Inside a Package

Classes from another package can be accessed using import statements or fully qualified class names.

1. Import a Single Class

import java.util.ArrayList;

2. Import All Accessible Types from a Package

import java.util.*;

The wildcard imports accessible types from java.util, but not from its subpackages.

Java
import java.util.ArrayList;
public class Geeks{
    public static void main(String[] args){

        // Using imported class
        ArrayList<Integer> list = new ArrayList<>();
        list.add(10);
        list.add(20);

        // Using fully qualified class name
        java.util.HashMap<String, Integer> map =
                new java.util.HashMap<>();
        map.put("Age", 25);
        System.out.println(list);
        System.out.println(map);
    }
}

Output
[10, 20]
{Age=25}

Explanation: ArrayList is accessed using an import statement, while HashMap is accessed using its fully qualified class name. Both collections use generics for type safety.

Access Modifiers and Packages

Packages work with Java access modifiers to control the visibility of classes and their members.

  • public members are accessible from other packages when their declaring types are accessible.
  • protected members are accessible within the same package and to subclasses in other packages, subject to Java's access rules.
  • Members with no access modifier have package-private access.
  • private members are accessible only within their enclosing class or permitted nested classes.
Comment