Package in Java
A package in Java is a collection of related classes, interfaces, and sub-packages. Packages are used to organize code, avoid naming conflicts, and provide access control.
Java provides built-in packages such as java.util, java.io, and java.lang. Programmers can also create their own packages.
Advantages of Packages
- Organizes related classes and interfaces.
- Avoids naming conflicts.
- Provides access control.
- Makes code easier to maintain.
- Supports code reusability.
- Helps organize large Java projects.
Creating a Package
A package is created using the package keyword.
Syntax
package packageName;
Example
package mypackage; public class Student { public void display() { System.out.println("Hello from Student class"); } }
The file can be stored in a directory such as:
mypackage/ Student.java
Using a Package
The import keyword is used to access classes from another package.
Example
import mypackage.Student; class Main { public static void main(String[] args) { Student s = new Student(); s.display(); } }
Output
Hello from Student class
Built-in Packages
Java provides many predefined packages.
| Package | Purpose |
|---|---|
java.lang | Basic Java classes such as String, Math, and System |
java.util | Utility classes such as ArrayList, Scanner, and HashMap |
java.io | Input and output operations |
java.net | Networking operations |
java.sql | Database connectivity |
java.time | Date and time API |
Example
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter your name: "); String name = input.nextLine(); System.out.println("Hello " + name); } }
Types of Packages
1. Built-in Packages
These are packages already provided by Java.
Example:
import java.util.ArrayList;
2. User-Defined Packages
These are packages created by programmers according to their requirements.
Example:
package mypackage;
Sub-Package
A package can contain another package called a subpackage.
Example:
com └── example ├── student └── teacher
A subpackage can be declared as:
package com.example.student;
Important Keywords
package
Defines the package to which a class belongs.
package mypackage;
import
Allows a class to use classes from another package.
import java.util.Scanner;
Package and Access Control
Packages work with Java access modifiers.
| Modifier | Same Class | Same Package | Subclass | Other Package |
|---|---|---|---|---|
public | ✓ | ✓ | ✓ | ✓ |
protected | ✓ | ✓ | ✓ | Limited |
| Default | ✓ | ✓ | ✗ | ✗ |
private | ✓ | ✗ | ✗ | ✗ |
Key Points
- A package is a group of related classes and interfaces.
- The
packagekeyword is used to create or declare a package. - The
importkeyword is used to access classes from another package. - Java has many built-in packages.
- Programmers can create user-defined packages.
- Packages help organize large programs and avoid naming conflicts.
Conclusion
Packages are an important feature of Java used to organize classes and interfaces, control access, avoid naming conflicts, and make large applications easier to manage.