Learn Java
import com.learnjava.packagesstandardlibrary.CustomPackages;

Creating Custom Packages in Java

@Concept

A custom package is declared with a package statement as the very first non-comment line of a file — package com.example.util; — and that declaration must match the actual directory structure the file lives in on disk: a class in package com.example.util needs to sit in a com/example/util folder relative to the project's source root, or the compiler won't find it correctly. Package names conventionally use reverse-domain notation, com.company.project, specifically to keep names globally distinct across different organizations' code, and follow an all-lowercase convention to visually distinguish them from class names, which are conventionally PascalCase.

Organizing code into packages beyond just avoiding naming collisions also groups genuinely related classes together logically. Package-private access, the default when no modifier is written, becomes a meaningful tool once real packages are in play, letting a package's internal helper classes stay hidden from code outside that package entirely.

@HowItWorks
1

The package declaration

package com.example.util; must be the very first non-comment line of the file.

2

Matching the directory structure

A class in package com.example.util needs to physically sit in a com/example/util folder.

3

Reverse-domain naming convention

com.company.project keeps package names globally distinct across different organizations.

4

All-lowercase convention

Package names are conventionally all-lowercase, visually distinguishing them from PascalCase class names.

5

Logical grouping

Packages organize genuinely related classes together, beyond just avoiding naming collisions.

@Example
package com.example.util;
 
public class MathHelper {
public static int square(int n) {
return n * n;
}
 
public static void main(String[] args) {
System.out.println("Square of 5: " + square(5));
}
}
Output
Square of 5: 25
1package com.example.util; is the very first line, declaring which namespace this class belongs to.
2This source file must physically sit inside a com/example/util directory for the compiler to find it correctly there.
3square(5) is called directly within the same class, needing no import since it's calling its own package's own code.
4The single println call prints the computed result as one line.
@UseCases
Organizing a growing codebase's related classes into logically-grouped namespaces
Avoiding a naming collision between a custom class and an unrelated class sharing the same simple name
Following reverse-domain naming convention when publishing a library others might depend on
Debugging a compiler error caused by a package declaration not matching the file's actual directory location

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.