What are Java Packages?

A package in Java is a way to organize related classes and interfaces into a single unit. Think of it like a folder on your computer that contains related files. Packages help keep your code organized, prevent naming conflicts, and make it easier to find and use classes.

📦

Package = Container

Just like you organize your school subjects into different folders (Math, Science, English), Java packages organize classes into logical groups. For example, all classes related to input/output operations are in the java.io package.

Why Use Packages?

Packages provide several important benefits that make your code better organized and easier to manage.

🗂️

Organization

Group related classes together. For example, all database-related classes can be in a database package.

🛡️

Avoid Name Conflicts

Two classes can have the same name if they're in different packages. For example, java.util.Date and java.sql.Date are different classes.

🔒

Access Control

Packages provide an additional level of access control. You can make classes accessible only within the same package.

Package Syntax

Creating and using packages in Java involves two main keywords: package and import.

1. Creating a Package

Use the package keyword at the very beginning of your Java file (before any class declaration). This statement must be the first line of code in the file.

package mypackage;

public class MyClass {
    // class code here
}

Note: The file must be saved in a folder named mypackage to match the package name.

2. Importing a Package

Use the import keyword to use classes from other packages in your code.

import mypackage.MyClass;

// Or import all classes from a package
import mypackage.*;

Note: The * wildcard imports all classes from the package, but it's better to import only what you need.

Simple Example

Let's create a simple package with a class and then use it in another class. This example demonstrates the complete workflow of creating and using packages.

Step 1: Create a Package with a Class

Create a file named Calculator.java in a folder named mathtools:

package mathtools;

public class Calculator {
    // Method to add two numbers
    public int add(int a, int b) {
        return a + b;
    }
    
    // Method to subtract two numbers
    public int subtract(int a, int b) {
        return a - b;
    }
    
    // Method to multiply two numbers
    public int multiply(int a, int b) {
        return a * b;
    }
    
    // Method to divide two numbers
    public double divide(int a, int b) {
        if (b != 0) {
            return (double) a / b;
        } else {
            System.out.println("Cannot divide by zero!");
            return 0;
        }
    }
}

Step 2: Use the Package in Another Class

Create a file named Main.java in your main project folder:

import mathtools.Calculator;

public class Main {
    public static void main(String[] args) {
        // Create an object of Calculator class
        Calculator calc = new Calculator();
        
        // Use the methods
        System.out.println("Addition: " + calc.add(10, 5));
        System.out.println("Subtraction: " + calc.subtract(10, 5));
        System.out.println("Multiplication: " + calc.multiply(10, 5));
        System.out.println("Division: " + calc.divide(10, 5));
    }
}

💡 Output

Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2.0

📁 Folder Structure

Your project folder should look like this:

MyProject/
├── Main.java
└── mathtools/
    └── Calculator.java

Built-in Java Packages

Java comes with many pre-built packages that provide useful classes for common tasks. Here are some of the most commonly used packages in ISC Class 11.

Package Name Description Common Classes
java.lang Fundamental classes (automatically imported) String, Math, System, Integer, Double
java.util Utility classes (collections, date/time) Scanner, ArrayList, HashMap, Date
java.io Input/Output operations BufferedReader, FileReader, PrintWriter
java.awt GUI components (windows, buttons) Frame, Button, Label, TextField
java.applet Creating applets Applet

Special Note: java.lang

The java.lang package is special because it's automatically imported in every Java program. This means you don't need to write import java.lang.*; to use classes like String, Math, or System. They're always available!

Key Points to Remember

  • A package is a collection of related classes and interfaces.
  • The package statement must be the first line in a Java file.
  • Use import to use classes from other packages.
  • The folder structure must match the package name.
  • java.lang is automatically imported in every Java program.
  • Packages help organize code, prevent naming conflicts, and provide access control.