01 / 21

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.

Java

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

Organization
Group related classes so large projects stay manageable and navigable.
Name Conflicts
Two classes with the same name can coexist in different packages without collision.
Access Control
Package-private (default) access restricts visibility to classes in the same package.

Package Hierarchy as a Tree

com.myapp com.myapp.model com.myapp.util com.myapp.view User.java Order.java Logger.java MainView.java Each package maps to a folder on disk: com/myapp/model/User.java

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).

package com.myapp.model; public class User { // class body }

Naming Convention Rules

1
All lowercase. Package names use only lowercase letters: com.example.utils, never com.Example.Utils.
2
Reverse domain prefix. Start with your organization's reversed internet domain: com.google, org.apache.
3
Dots as separators. Each segment between dots maps to one directory level on disk.
4
No Java keywords. Segments must not be reserved words like int or class. Append an underscore if needed: com.example.int_.

Reverse Domain Convention

Website Domain example.com Package Prefix com.example Full Package Name com.example.inventory Guarantees worldwide uniqueness

import vs. Fully Qualified Names

import statement
Declare at the top of your file, then use the short class name everywhere.
Fully qualified
Use the complete path inline: useful when two packages have a class with the same name.
// Option A: import import java.util.ArrayList; ArrayList<String> list = new ArrayList<>(); // Option B: fully qualified java.util.ArrayList<String> list = new java.util.ArrayList<>();

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

1
Choose a name. Follow the reverse-domain convention: com.yourname.project.module.
2
Create matching folders. Each dot becomes a subdirectory: com/yourname/project/module/.
3
Add the package statement. First line of every source file in that folder: package com.yourname.project.module;
4
Compile with the path. Use javac -d out src/com/yourname/project/module/MyClass.java.
5
Import from other files. Other classes can now use import com.yourname.project.module.MyClass;.

Example: Library System

A school library application with separate packages for data models and services.

// File: com/schoollib/model/Book.java package com.schoollib.model; public class Book { private String title; private String author; public Book(String title, String author) { this.title = title; this.author = author; } public String getTitle() { return title; } }

Example: Using a Different Package

// File: com/schoollib/service/Library.java package com.schoollib.service; import com.schoollib.model.Book; import java.util.ArrayList; public class Library { private ArrayList<Book> catalog = new ArrayList<>(); public void addBook(Book b) { catalog.add(b); } }

Putting It Together

// File: com/schoollib/app/Main.java package com.schoollib.app; import com.schoollib.model.Book; import com.schoollib.service.Library; public class Main { public static void main(String[] args) { Library lib = new Library(); lib.addBook(new Book("1984", "Orwell")); System.out.println("Book added!"); } }

Key Built-in Packages

java.lang
Auto-imported. Contains String, Math, System, Object: the core of every program.
java.util
Collections framework: ArrayList, HashMap, Scanner, and utility classes.
java.io
Input/output: File, BufferedReader, PrintWriter for reading and writing data.

Common Mistakes

✕ Wrong Case
Using uppercase letters like Com.MyApp instead of com.myapp. Package names must be all lowercase.
✕ Mismatched Folder
Declaring package com.app but placing the file in a src/ root. The folder path must match the package name exactly.
✕ Missing Statement
Forgetting the package declaration. The class ends up in the default package and becomes unimportable.
✕ Wildcard Abuse
Using import java.util.* and import java.awt.* together causes List ambiguity: both packages define a List class.

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?