codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

31 / 65 topics
24Java OOP25Java Classes/Objects26Java Class Attributes27Java Class Methods28Java Constructors29Java Modifiers30Java Encapsulation31Java Packages / API
Tutorials/Java Programming/Java Packages / API
☕Java Programming

Java Packages / API

Updated 2026-05-12
30 min read

Java Packages / API

Introduction

In Java, a package is a way of organizing classes and interfaces into a single unit. This helps in managing large projects by grouping related classes together and avoiding naming conflicts. Understanding packages is crucial for leveraging the Java Standard Library (API) and creating your own organized codebase.

Core Content

Built-in Packages

Java comes with a rich set of built-in packages, collectively known as the Java API (Application Programming Interface). These packages provide a wide range of functionalities that can be used in various applications. Some commonly used built-in packages include:

  • java.lang: Contains fundamental classes like String, Object, and Exception.
  • java.util: Provides utility classes for collections, date and time manipulation, and more.
  • java.io: Includes classes for input and output operations.
  • java.net: Offers classes for networking capabilities.

To use classes from these packages, you typically need to import them using the import keyword. For example:

Main.java
1import java.util.ArrayList;
2
3public class Main {
4 public static void main(String[] args) {
5 ArrayList<String> list = new ArrayList<>();
6 list.add("Hello");
7 System.out.println(list);
8 };
9}
Terminal
$ javac Main.java
$ java Main
Output
[Hello]

Import Keyword

The import keyword is used to include classes or interfaces from other packages in your Java program. There are two types of import statements:

  1. Single-type import: Imports a single class.
  2. On-demand import: Imports all public classes and interfaces from a package.

Single-Type Import

Main.java
1import java.util.ArrayList;
2
3public class Main {
4 public static void main(String[] args) {
5 ArrayList<String> list = new ArrayList<>();
6 list.add("Hello");
7 System.out.println(list);
8 }
9}
Terminal
$ javac Main.java
$ java Main
Output
[Hello]

On-Demand Import

Main.java
1import java.util.*;
2
3public class Main {
4 public static void main(String[] args) {
5 ArrayList<String> list = new ArrayList<>();
6 list.add("Hello");
7 System.out.println(list);
8 }
9}
Terminal
$ javac Main.java
$ java Main
Output
[Hello]

Creating User-Defined Packages

You can create your own packages to organize your classes. This is particularly useful in large projects where multiple developers are involved.

Steps to Create a Package

  1. Create a Directory: The package name should match the directory structure.
  2. Declare the Package: Use the package keyword at the beginning of each Java file within that package.

Example:

Suppose you want to create a package named com.example.

  1. Create Directory Structure: src/ └── com/ └── example/ └── Main.java

  2. Declare the Package in Main.java:

src/com/example/Main.java
1package com.example;
2
3public class Main {
4 public static void main(String[] args) {
5 System.out.println("Hello from com.example package!");
6 }
7}
  1. Compile and Run the Program:
    • Navigate to the src directory.
    • Compile the program using the fully qualified name of the class.
Terminal
$ javac com/example/Main.java
$ java com.example.Main
Output
Hello from com.example package!

Importing User-Defined Packages

To use classes from a user-defined package, you need to import them just like built-in packages.

src/com/example/User.java
1package com.example;
2
3public class User {
4 private String name;
5
6 public User(String name) {
7 this.name = name;
8 }
9
10 public void display() {
11 System.out.println("User: " + name);
12 }
13}
src/com/example/Main.java
1package com.example;
2
3import com.example.User;
4
5public class Main {
6 public static void main(String[] args) {
7 User user = new User("Alice");
8 user.display();
9 }
10}
Terminal
$ javac com/example/User.java
$ javac com/example/Main.java
$ java com.example.Main
Output
User: Alice

Practical Example

Let's create a simple application that demonstrates the use of built-in and user-defined packages.

Directory Structure: src/ ├── com/ │ └── example/ │ ├── Main.java │ └── User.java └── utils/ └── MathUtils.java

User.java:

src/com/example/User.java
1package com.example;
2
3public class User {
4 private String name;
5
6 public User(String name) {
7 this.name = name;
8 }
9
10 public void display() {
11 System.out.println("User: " + name);
12 }
13}

MathUtils.java:

src/utils/MathUtils.java
1package utils;
2
3public class MathUtils {
4 public static int add(int a, int b) {
5 return a + b;
6 }
7
8 public static int subtract(int a, int b) {
9 return a - b;
10 }
11}

Main.java:

src/com/example/Main.java
1package com.example;
2
3import com.example.User;
4import utils.MathUtils;
5
6public class Main {
7 public static void main(String[] args) {
8 User user = new User("Alice");
9 user.display();
10
11 int sum = MathUtils.add(10, 5);
12 System.out.println("Sum: " + sum);
13
14 int difference = MathUtils.subtract(10, 5);
15 System.out.println("Difference: " + difference);
16 }
17}

Compile and Run:

Terminal
$ javac src/com/example/User.java
$ javac src/utils/MathUtils.java
$ javac src/com/example/Main.java
$ java com.example.Main
Output
User: Alice
Sum: 15
Difference: 5

Summary

  • Packages in Java help organize classes and interfaces, making large projects manageable.
  • Built-in packages like java.lang, java.util, and java.io provide essential functionalities.
  • The import keyword is used to include classes from other packages.
  • You can create your own user-defined packages by organizing your code into directories and using the package keyword.

What's Next?

Now that you have a good understanding of Java packages, let's move on to Java Inheritance, where we'll explore how classes can inherit properties and behaviors from other classes. This will further enhance your ability to create modular and reusable code.


PreviousJava EncapsulationNext Java Inheritance

Recommended Gear

Java EncapsulationJava Inheritance