Packages &
Naming Conventions
Learn how Java organizes classes into packages, how to create your own, and how to follow industry-standard naming rules for clean, professional code.
What Is a Package?
A package in Java is a namespace that groups related classes and interfaces together: like a folder on your computer that organizes files by purpose. Every class you write belongs to some package.
Why Packages Matter
Package Hierarchy as a Tree
The package Statement
Every Java source file that belongs to a package must declare it as the very first line of code (before any import or class declaration).
Naming Convention Rules
Reverse Domain Convention
import vs. Fully Qualified Names
Wildcard * vs. Specific Imports
| Specific Import | Wildcard Import | |
|---|---|---|
| Syntax | import java.util.List; | import java.util.*; |
| Clarity | Immediately shows which classes are used | Hides actual dependencies |
| Name Conflicts | Easy to spot and resolve | Can cause ambiguity errors |
| Best Practice | ✓ Preferred | Acceptable for quick prototyping |
How to Create a Package
Example: Library System
A school library application with separate packages for data models and services.
Example: Using a Different Package
Putting It Together
Key Built-in Packages
Common Mistakes
Quick Reference Table
| Concept | Syntax / Rule | Example |
|---|---|---|
| Declare a package | package name; | package com.app.util; |
| Import a class | import pkg.Class; | import java.util.List; |
| Wildcard import | import pkg.*; | import java.util.*; |
| Naming rule | All lowercase, reverse domain | org.apache.commons |
| Folder mapping | Dots become / | org/apache/commons/ |
| Auto-imported | java.lang only | String, Math, System |
Question 1 of 5
Which of the following is a correctly formatted Java package name?
Question 2 of 5
If your organization's website is cooltools.org, what should your package prefix be?
Question 3 of 5
Which package is automatically imported into every Java source file?
Question 4 of 5
A file declares package com.shop.data; at the top. In which directory must the file be located?
Question 5 of 5
What problem can arise from using two wildcard imports that both contain a class with the same name?