BCA / B.Tech 12 min read

Packages in Java

Packages in Java:


What is a Package?

A Package in Java is a group of classes and interfaces. It helps in organizing and managing code.

In simple terms: Just as we create folders on our computer to store files so that everything is well-managed, packages are used in Java for the same purpose.

If the code is small, there is no problem. But in large projects, it is necessary to use packages to manage the code properly.

Types of Packages in Java:

1. Built-in Package (Predefined Package)

Java has pre-built packages that we can use directly.
Examples:
  • java.util (for ArrayList, Scanner, etc.)
  • java.io (for Input/Output operations)
  • java.sql (for Database operations)
2. User-defined Package

We can also create our own packages to keep our code well-organized. The `package` keyword is used for this.

Built-in Package Example:

Example of Scanner Class (from java.util package)
import java.util.Scanner; // Imported the java.util package

public class UserInput {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in); // Created a Scanner class object
        System.out.println("Enter your name:");
        String name = sc.nextLine();
        System.out.println("Your name: " + name);
        sc.close();
    }
}
Explanation:
  • `import java.util.Scanner;` → We imported the Scanner class.
  • `Scanner sc = new Scanner(System.in);` → Used the Scanner class to take user input.

User-defined Package (Creating your own package):

Step 1: Create the package
package mypackage; // The package name is "mypackage"

public class MyClass {
    public void showMessage() {
        System.out.println("This is a user-defined package!");
    }
}
Explanation:
  • `package mypackage;` → This is the name of the package.
  • `MyClass` → We will use this class in another file later.

Step 2: Use this package in another file
import mypackage.MyClass; // Imported our created package

public class Test {
    public static void main(String[] args) {
        MyClass obj = new MyClass(); // Created an object of MyClass
        obj.showMessage(); // Called the method
    }
}
Explanation:
  • `import mypackage.MyClass;` → Imported the package we created.
  • `MyClass obj = new MyClass();` → Created its object and called the method.

Default Package (What happens if no package is specified?)

If we do not define any package, the default package is used.
class DefaultPackageExample {
    public static void main(String args[]) {
        System.out.println("This is an example of the default package!");
    }
}
Explanation:
  • No package is written in this file.
  • This means it is in the default package.
Static Import (Using a class's methods directly)

If we want to use the methods of a class without creating an object, we use a static import.
import static java.lang.Math.*; // Statically imported the Math class

public class StaticImportExample {
    public static void main(String[] args) {
        System.out.println("Square Root of 25: " + sqrt(25)); // Used sqrt() directly
        System.out.println("Power of 2^3: " + pow(2, 3)); // Used pow() directly
    }
}
Explanation:
  • `import static java.lang.Math.*;` → Imported all static methods of the Math class.
  • Now, instead of `Math.sqrt()`, we can just write `sqrt()`.

Advantages of Packages in Java:

  • Code Organization: Manages large projects properly.
  • Code Reusability: A package created once can be used anywhere.
  • Avoid Name Conflicts: Classes with the same name can be kept in different packages.
  • Access Control: Packages are used for data security and access control.
  • Benefit of Built-in Library: Makes coding easier by using Java's pre-made packages.