Java Packages & API
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

Java Packages & API

Harine

 Java Packages & API

In Java, a package is a namespace that groups related classes and interfaces together.

Conceptually, it works like a folder in a file system: it organizes code, prevents naming conflicts,

and improves maintainability in large applications.

Packages in Java fall into two main categories:

  • Built-in packages — provided by the Java standard library (Java API)
  • User-defined packages — created by developers to organize their own code

What Is the Java API?

The Java API (Application Programming Interface) is a vast collection of prewritten classes and interfaces included with the Java Development Kit (JDK). It provides ready-to-use functionality for:

  • Input and output operations
  • Data structures and collections
  • Networking
  • Date and time handling
  • Database connectivity
  • Utility operations

The official documentation is maintained by Oracle and available at:

https://docs.oracle.com/javase/8/docs/api/

The API is organized into packages, and each package contains multiple related classes.

Importing Packages and Classes in Java

To use classes from another package, Java provides the import keyword.

Import syntax

import packageName.ClassName; // Import a single class

import packageName.*; // Import all classes in a package

Importing a Single Class — Example

The Scanner class (from java.util) allows programs to read user input from the console

Example: Reading user input with Scanner

import java.util.Scanner;

public class UserProfileApp {

  public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.print("Enter your name: ");

    String name = input.nextLine();

    System.out.print("Enter your age: ");

    int age = input.nextInt();

    System.out.println("Welcome " + name + "! You are " + age + " years old.");

    input.close();

  }

}

Explanation

  •  java.util → package
  •  Scanner → class
  •  nextLine() and nextInt() → methods of the Scanner class

Importing an Entire Package

Instead of importing classes individually, you can import all classes from a package using *.

import java.util.*;

This makes all classes from java.util available, such as:

  • Scanner
  • Random
  • ArrayList
  • Date

Best practice: Import only the classes you need to avoid unnecessary namespace pollution and improve readability.

Creating User-Defined Packages in Java

Developers often create their own packages to organize application code logically — especially in large projects.

Step 1: Define the package

Every Java source file belonging to a package must start with the package statement.

Example: Creating a utility package

File: MathUtility.java

package com.example.utility;

public class MathUtility {

  public static int square(int number) {
    return number * number;
}

public static int cube(int number) {
  return number * number * number;
  }
}

Step 2: Compile the package

Use the -d option to instruct the compiler to create the appropriate directory structure.

javac -d . MathUtility.java

This generates folders:

com/
└── example/
└── utility/
└── MathUtility.class

Step 3: Use the user-defined package

Now create another class that imports and uses this package.

File: PackageDemo.java

import com.example.utility.MathUtility;
public class PackageDemo {
  public static void main(String[] args) {
    int value = 5;

    System.out.println("Square: " + MathUtility.square(value));
    System.out.println("Cube: " + MathUtility.cube(value));
  }
}

Compile and run:

javac PackageDemo.java
java PackageDemo

Output

Square: 25
Cube: 125

Package Naming Conventions (Best Practices)

Java follows standard naming rules to avoid conflicts:

  • Use lowercase letters
  • Use reversed domain names for uniqueness
  • Avoid spaces and special characters

Examples

com.company.project
org.example.app
In.education.portal

Running a Class Inside a Package

If a class belongs to a package, run it using its fully qualified name:

java com.example.utility.MathUtility
Tags
Our website uses cookies to enhance your experience. Learn More
Accept !