Java Packages

A Java Package is a way to group related classes, interfaces, and sub-packages into namespaces, helping to organize code into a modular structure. Packages are used to avoid naming conflicts, provide access control, and manage large codebases more efficiently. Java’s package system also facilitates reusability and security.

Why Use Packages?

1. Namespace Management: Packages help avoid name conflicts by grouping classes with similar functionality.

2. Code Organization: Packages help to logically group related classes, making code easier to navigate.

3. Access Control: Packages provide a mechanism for controlling access to classes and methods. Classes in the same package can access each other’s members more easily.

4. Reusability: Classes in a package can be reused across different applications.

Types of Java Packages

1. Predefined Packages: These are pre-defined packages provided by Java.

  1. java.util (for collections like ArrayList, HashMap).
  2. java.io (for Input/Output operations).
  3. java.lang (includes fundamental classes like String, Math).

2. User-defined Packages: These are custom packages created by the programmer to organize their code. You can create your own packages to group related classes together.

Create Custom Package

Step 1. Create a Package

Let’s create a simple user-defined package called customPackage.math.


// File: customPackage/math/Calculator.java
package customPackage/math;  // Define the package

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;
    }
}

Explanation:

  1. We create a class Calculator and define it inside the package customPackage.math using the package keyword.
  2. The class provides methods to perform basic arithmetic operations like add(), subtract(), and multiply().

Step 2: Use the Package in Another Class

Now, let’s use the Calculator class from the com.example.math package in a different Java class.


// File: Main.java
import customPackage.math.Calculator;  // Import the Calculator class from the package

public class Main {
    public static void main(String[] args) {
        // Create an object of the Calculator class
        Calculator calc = new Calculator();

        // Perform operations using the Calculator object
        int sum = calc.add(20, 10);
        int difference = calc.subtract(20, 10);
        int product = calc.multiply(20, 10);

        // Output the results
        System.out.println("Sum: " + sum);  // Output: Sum: 30
        System.out.println("Difference: " + difference);  // Output: Difference: 10
        System.out.println("Product: " + product);  // Output: Product: 200
    }
}

Explanation:

  1. In this class, we import the Calculator class from the customPackage.math package using the import keyword.
  2. We create an instance of Calculator and call its methods to perform arithmetic operations.
  3. The results are printed to the console.

Step 3: Compile and Run the Code

1. Compile the Calculator class:

  1. Open the terminal and navigate to the directory where the com/example/math/Calculator.java file is located.
  2. Use the javac compiler to compile the class:

javac com/example/math/Calculator.java

2. Compile the Main class:

  1. Navigate to the directory where the Main.java file is located.
  2. Compile the Main.java file:

javac Main.java

3. Run the program:

To run the program, use the java command, making sure to include the package path:


java Main

Output:

Sum: 30
Difference: 10
Product: 200