The Wayback Machine - https://web.archive.org/web/20251217041808/https://www.geeksforgeeks.org/java/packages-in-java/
Open In App

Java Packages

Last Updated : 21 Nov, 2025
Comments
Improve
Suggest changes
294 Likes
Like
Report

A package in Java is a mechanism to group related classes, interfaces, and sub-packages into a single unit. Packages help organize large applications, avoid naming conflicts, provide access protection, and make code modular and maintainable.

  • Avoiding name conflicts (two classes with the same name can exist in different packages)
  • Providing access control using public, protected, and default access
  • Reusability: packaged code can be imported and used anywhere
  • Encouraging modular programming

Types of Java Packages

packages_
Types of Package

1. Built-in Packages

Built-in Packages comprise a large number of classes that are part of the Java API. Some of the commonly used built-in packages are:

  • java.lang: Contains language support classes(e.g, classes that define primitive data types, math operations). This package is automatically imported.
  • java.io: Contains classes for supporting input/output operations.
  • java.util: Contains utility classes that implement data structures such as Linked Lists and Dictionaries, as well as support for date and time operations.
  • java.applet: Contains classes for creating Applets.
  • java.awt: Contains classes for implementing the components for graphical user interfaces (like buttons, menus, etc). 6)

Example: Using java.util.Random (Built-in Package)

Java
import java.util.Random;   // built-in package

public class GFG{
    
    public static void main(String[] args) {
        
        // using Random class
        Random rand = new Random();   

        // generates a number between 0–99
        int number = rand.nextInt(100);  

        System.out.println("Random number: " + number);
    }
}

Output
Random number: 59

2. User-defined Packages

User-defined Packages are the packages that are defined by the user.

Example:

Java
package com.myapp;

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

To use it in another class:

Java
import com.myapp.Helper;

public class Test {
    public static void main(String[] args) {
        Helper.show();
    }
}

Forder Structure

pp
Forder Structure

Accessing Classes Inside a Package

In Java, we can import classes from a package using either of the following methods:

1 Import a Single Class

import java.util.Vector;

This imports only the Vector class from the java.util package.

2. Import all classes from a package:

import java.util.*;

This imports all classes and interfaces from the java.util package but does not include sub-packages.

Example: Import the Vector class

Java
import java.util.Vector;

public class Geeks {
  
    public Geeks() {
      
        // java.util.Vector is imported, We are able to access it directly in our code.
        Vector v = new Vector();

       
        java.util.ArrayList l = new java.util.ArrayList();
        l.add(3);
        l.add(5);
        l.add(7);
        
        System.out.println(l);
    }

    public static void main(String[] args) {
      
       
        new Geeks();
    }
}

Output
[3, 5, 7]

Access Modifiers and Packages

Packages directly influence Java access levels:

packages_
Access Modifiers and Packages
Suggested Quiz
5 Questions

Which of the following is are true about packages in Java?

1) Every class is part of some package. 
2) All classes in a file are part of the same package.
3) If no package is specified, the classes in the file
go into a special unnamed package
4) If no package is specified, a new package is created with
folder name of class and the class is put in this package.
  • A

    Only 1, 2 and 3

  • B

    Only 1, 2 and 4

  • C

    Only 4

  • D

    Only 1 and 3

Explanation:

In Java, classes are either in the default unnamed package or a specified package, and multiple classes in a file share the same package unless specified otherwise.

What will happen if two classes in different packages have the same name and are imported in a Java file?


  • A

    Compilation error due to ambiguity.

  • B

    The last imported class is used.


  • C

    The first imported class is used.

  • D

    Java automatically renames one class.

Explanation:

Java does not allow ambiguity in class names across packages. Fully qualified names must be used in such cases.


What is the purpose of using packages in Java?

  • A

    To reduce code size

  • B

    To improve memory management

  • C

    To improve runtime performance

  • D

    To avoid name conflicts and maintain code organization

Explanation:

Packages help structure code neatly and allow classes with the same name to exist in different modules.

Which keyword is used to import built-in or user-defined packages?

  • A

    include

  • B

    package

  • C

    import

  • D

    extends

Explanation:


Which built-in Java package would you use if you want to create a GUI window and display a message?

  • A

    java.util

  • B

    java.sql

  • C

    java.awt

  • D

    java.net

Explanation:

java.awt provides classes for GUI and graphics (like Frame, Label, Button).

Image
Quiz Completed Successfully
Your Score :   2/5
Accuracy :  0%
Login to View Explanation
1/5 1/5 < Previous Next >

Article Tags :

Explore