- Understand packages and their importance
- Learn to create and use packages
- Master import statements and naming conventions
Introduction to Packages in Java
Packages organize related classes, interfaces, and sub-packages in Java. They're like folders on your computer, but for code.
What are Packages?
A package is a namespace that groups related classes and interfaces. If you have 50 classes, you don't dump them all in one place—you organize them into logical groups.
Why Use Packages?
- Organization: Group related classes together
- Name Conflicts: Avoid naming conflicts between classes
- Access Control: Control visibility of classes and members
- Reusability: Make classes easier to find and use
- Maintainability: Easier to maintain and understand large codebases
Creating and Using Packages
Package Declaration
To create a package, use the package keyword at the top of your Java file:
package com.example.myapp;
public class MyClass {
// Class implementation
}
Package Naming Conventions
Java packages follow reverse domain name conventions:
// Good package names
com.company.project
org.organization.library
edu.university.department
// Avoid
myPackage
utils
helpers
Directory Structure
Package structure must match the directory structure:
src/
├── com/
│ ├── example/
│ │ ├── myapp/
│ │ │ ├── MyClass.java
│ │ │ └── AnotherClass.java
│ │ └── utils/
│ │ └── Helper.java
Importing Classes
Single Class Import
import java.util.ArrayList;
public class MyClass {
ArrayList<String> list = new ArrayList<>();
}
Import All Classes from Package
import java.util.*;
public class MyClass {
ArrayList<String> list = new ArrayList<>();
HashMap<String, Integer> map = new HashMap<>();
}
Fully Qualified Names
You can also use fully qualified names without imports:
public class MyClass {
java.util.ArrayList<String> list = new java.util.ArrayList<>();
}
Package Access
Default (Package-Private) Access
Classes and members with no access modifier are accessible within the same package:
package com.example.myapp;
class PackagePrivateClass {
int packagePrivateField;
void packagePrivateMethod() {}
}
Public Access
Public classes are accessible from anywhere:
package com.example.myapp;
public class PublicClass {
public void publicMethod() {}
}
Common Package Patterns
Layered Architecture
com.example.myapp
├── controller/
├── service/
├── repository/
├── model/
└── util/
Feature-Based Organization
com.example.myapp
├── user/
│ ├── UserController.java
│ ├── UserService.java
│ └── UserRepository.java
├── product/
│ ├── ProductController.java
│ ├── ProductService.java
│ └── ProductRepository.java
Built-in Java Packages
Java provides many built-in packages:
java.lang: Fundamental classes (String, Object, etc.)java.util: Utility classes (Collections, Date, etc.)java.io: Input/Output operationsjava.net: Networking operationsjava.sql: Database operations
Package Compilation
Compiling with Packages
# Compile from source root
javac com/example/myapp/MyClass.java
# Run with fully qualified name
java com.example.myapp.MyClass
Using Classpath
# Compile with classpath
javac -cp . com/example/myapp/MyClass.java
# Run with classpath
java -cp . com.example.myapp.MyClass
Packages organize Java code, prevent naming conflicts, and provide access control. Use reverse domain names (com.company.project), keep them lowercase, and match your directory structure. They're essential for any project bigger than a handful of classes.
